[Tutor] telnetlib's read_very_eager() method?
I'm trying to programmatically create a telnet session. Within the interactive environment, this appears to work very well as server messages can be saved as strings: $ python Python 2.7.1 (r271:86832, Sep 3 2011, 01:32:33) [GCC 4.2.1 20070719 ] on openbsd5 Type "help", "copyright", "credits" or "license" for more information. >>> import telnetlib >>> tn = telnetlib.Telnet('gmail-smtp-in.l.google.com', 25) >>> s = tn.read_very_eager() >>> print s 220 mx.google.com ESMTP r70si1582241yhm.54 >>> tn.write("helo\n") >>> s = tn.read_very_eager() >>> print s 250 mx.google.com at your service >>> tn.write("quit\n") >>> s = tn.read_very_eager() >>> print s 221 2.0.0 closing connection r70si1582241yhm.54 >>> $ These server response can then be parsed to determine what commands need to be provided next. Yet when I execute the same methods in a script, I am getting nothing in response: $ cat script.py #!/usr/bin/env python import telnetlib if __name__ == '__main__': print 'begin' tn = telnetlib.Telnet('gmail-smtp-in.l.google.com', 25) s = tn.read_very_eager() print s tn.write("helo\n") s = tn.read_very_eager() print s tn.write("quit\n") s = tn.read_very_eager() print s print 'end' $ python script.py begin end $ What do I need to do to emulate the IDE environment? If I am needing to capture stdout, it isn't readily apparent. Any insight you can share would be greatly appreciated. Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] telnetlib's read_very_eager() method?
On 14 September 2011 20:44, James Hartley wrote: > #!/usr/bin/env python > > import telnetlib > > if __name__ == '__main__': > print 'begin' > tn = telnetlib.Telnet('gmail-smtp-in.l.google.com', 25) > s = tn.read_very_eager() > print s > tn.write("helo\n") > s = tn.read_very_eager() > print s > tn.write("quit\n") > s = tn.read_very_eager() > print s > print 'end' > $ python script.py > begin > > > > end > $ > > What do I need to do to emulate the IDE environment? If I am needing to > capture stdout, it isn't readily apparent. > Make it go slower or force it to read *some* data at least. Interactively there's plenty of time for data to arrive for read_very_eager() to read. When run as a batch your computer tries to read and continue before any data's been returned from google. Hence, try using read_some() instead of read_very_eager(). Aside: If you want to interact with a mail server there's probably better modules to be using than the telnet module. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] telnetlib's read_very_eager() method?
On Wed, Sep 14, 2011 at 12:54 PM, Walter Prins wrote: > Hence, try using read_some() instead of read_very_eager(). > read_some() captures what I was hoping to catch. Thanks! > > Aside: If you want to interact with a mail server there's probably better > modules to be using than the telnet module. > > Do you have any suggestions? Thank you again for your quick reply. Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] telnetlib's read_very_eager() method?
On 14 September 2011 21:03, James Hartley wrote: > On Wed, Sep 14, 2011 at 12:54 PM, Walter Prins wrote: > >> Hence, try using read_some() instead of read_very_eager(). >> > > read_some() captures what I was hoping to catch. Thanks! > > >> >> Aside: If you want to interact with a mail server there's probably better >> modules to be using than the telnet module. >> >> > Do you have any suggestions? > The smtplib module: http://docs.python.org/library/smtplib.html Cheers, Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] telnetlib's read_very_eager() method?
On Wed, Sep 14, 2011 at 1:13 PM, Walter Prins wrote: > On 14 September 2011 21:03, James Hartley wrote: > >> On Wed, Sep 14, 2011 at 12:54 PM, Walter Prins wrote: >> > Aside: If you want to interact with a mail server there's probably better >> modules to be using than the telnet module. >> >>> >>> >> Do you have any suggestions? >> > > The smtplib module: http://docs.python.org/library/smtplib.html > Wow, I wasn't aware this existed. It appears to have what I was looking for, too. My goal was to validate email addresses in bulk, so this reduces the code I need to write. I also see that VRFY is disabled on some MX servers, so I may have to resort to telnetlib's read_some() yet. :-) Thanks, again for your comments. Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] making lists of prime numbers
hi list, i am trying the MIT opencourseware assignments. one was to find the 1000th prime. since this isn't actually my homework, I modified the solution as I would like to collect lists of primes and non-primes up to N, also some log() ratio to one comparison. here is what I came up with on paper: #!/usr/bin/env python import sys, os, math def main(a): notprime = [] primelist = [2,3] nprime = sys.argv[1] numcheck = 4 while len(primelist) < nprime: for i in range(2,numcheck-1): if numcheck % i == 0: print numcheck,'is not prime' notprime.append(numcheck) numcheck += 1 break if i == numcheck and numcheck % i !=0: print numcheck, 'is prime' primelist.append(numcheck) numcheck += 1 break TwotoN = 0 for j in primelist: TwotoN += log(j) print 'sum of logs of primes from 2 to', nprime,'is',TwotoN print 'the ratio of this sum to 1 is' %f % (float(TwotoN)/nprime) if __name__=='__main__': main(sys.argv[1]) my questions would be: am I passing arguments from the command line correctly? this seems to count by twos (going through both 'if statements' maybe?) I thought the 'break' would send control back to the 'while' basically, is there an obvious problem here or is it just completely wrong? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] making lists of prime numbers
On Thu, Sep 15, 2011 at 4:01 AM, c smith wrote: > hi list, i am trying the MIT opencourseware assignments. > one was to find the 1000th prime. > since this isn't actually my homework, I modified the solution as I would > like to collect lists of primes and non-primes up to N, also some log() > ratio to one comparison. > here is what I came up with on paper: > #!/usr/bin/env python > import sys, os, math > > def main(a): > notprime = [] > primelist = [2,3] > nprime = sys.argv[1] > numcheck = 4 > while len(primelist) < nprime: > for i in range(2,numcheck-1): > if numcheck % i == 0: > print numcheck,'is not prime' > notprime.append(numcheck) > numcheck += 1 > break > if i == numcheck and numcheck % i !=0: > print numcheck, 'is prime' > primelist.append(numcheck) > numcheck += 1 > break > TwotoN = 0 > for j in primelist: > TwotoN += log(j) > print 'sum of logs of primes from 2 to', nprime,'is',TwotoN > print 'the ratio of this sum to 1 is' %f % (float(TwotoN)/nprime) > if __name__=='__main__': > main(sys.argv[1]) > > my questions would be: > am I passing arguments from the command line correctly? > this seems to count by twos (going through both 'if statements' maybe?) > I thought the 'break' would send control back to the 'while' > basically, is there an obvious problem here or is it just completely wrong? > I'd say the obvious problem is your second if statement. Its guard will never be true. i goes through range(2,numcheck-1). The highest number in that range in numcheck-2, so i==numcheck will never be true. Even if you'd get i to equal numcheck somehow, numcheck % i would then ber equal to numcheck % numcheck, which equals 0, so numcheck % i != 0 would not be true. The obvious thing to do seems to be to get this code out of the for loop, without and if statement guarding it. Another thing that goes wrong is: nprime = sys.argv[1] You use nprime as an integer, but sys.argv[1] is a string. You should thus change this into nprime = int(sys.argv[1]) However, for the sake of reusability, it's better to have a function that gets the first n primes for n decided by the caller than to have a function that gets the first n primes for n always being the first argument, thus I would change it to: nprime = int(a) -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor