[Tutor] Please sent me the output of this code .Please include both cases if input correct and if input wrong .
# Embedded file name: re4.py import time flag = [102, 108, 97, 103, 123, 112, 121, 116, 104, 111, 110, 95, 114, 111, 99, 107, 115, 125] password = raw_input('Enter secret code to get secret password: ') sleep_hours = 1 print 'Going to sleep. Will check password after %d hours' % sleep_hours print 'If you want to check password faster, find a way to prevent this sleep' time.sleep(sleep_hours * 60 * 60) if password == 'strongpassword': print 'Correct! The secret password is %s' % ''.join(map(chr, flag)) else: print 'Booo wrong code! No secret password for you' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Beginner: Socket object and packets
Hi tutor, I am trying to locate the first blank line in the first received packet when pinging an internet server using a socket object. My assumption is there will be a mandatory blank line right after the http headers in accordance with the http protocol. Consider the following: import socket mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect( ('www.py4inf.com/code/', 80) ) mysock.send('GET http://www.py4inf.com/code/' + ' HTTP/1.0\n\n') data_str = mysock.recv(700) My question: Why is the following statement False when there is an actual blank line in the received packet: '\n\n' in data The statement 'any_string in data' works fine with any character except the double line drop i.e. '\n\n'. Attached full script for your consideration. Thanks in advance for your guidance, Marc ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Please sent me the output of this code .Please include both cases if input correct and if input wrong .
On 05/12/15 18:48, Deepak Nn wrote: > # Embedded file name: re4.py If you want to know the output run it. If the output you get is not what you expect tell us what you input, what you got out, what you expec6ed. Also tell us which OS and Python versions you are using. Otherwise all I can tell you is that the output will be some combination of the various print statements depending on what input you provide(assuming there are no code errors). > import time > flag = [102, > 108, > 97, > 103, > 123, > 112, > 121, > 116, > 104, > 111, > 110, > 95, > 114, > 111, > 99, > 107, > 115, > 125] > password = raw_input('Enter secret code to get secret password: ') > sleep_hours = 1 > print 'Going to sleep. Will check password after %d hours' % sleep_hours > print 'If you want to check password faster, find a way to prevent this > sleep' > time.sleep(sleep_hours * 60 * 60) > if password == 'strongpassword': > print 'Correct! The secret password is %s' % ''.join(map(chr, flag)) > else: > print 'Booo wrong code! No secret password for you' I suspect the question you really want to ask is about how the map() function works? It will apply the chr() function to each number in flags. >>> chr(102),chr(108),chr(97) ('f', 'l', 'a') And so on. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner: Socket object and packets
On 05/12/15 13:21, Marc Eymard wrote: > Hi tutor, > > I am trying to locate the first blank line in the first received packet > when pinging an internet server using a socket object. You need to be careful with your descriptions. ping is a very specific message and uses ICMP echo rather than TCP/IP and looks nothing like http. In another context it would be OK but when dealing with sockets you need to be precise about what you are actually sending. > My assumption is there will be a mandatory blank line right after the > http headers in accordance with the http protocol. > > Consider the following: > > import socket > mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) > mysock.connect( ('www.py4inf.com/code/', 80) ) > mysock.send('GET http://www.py4inf.com/code/' + ' HTTP/1.0\n\n') > data_str = mysock.recv(700) Is there any reason why you are using raw sockets rather than the httplib module which does most of this stuff for you (and probably has useful code you code study if you do want to do it this way)? > My question: > > Why is the following statement False when there is an actual blank line > in the received packet: > '\n\n' in data It looks like you are using Python v3. Remember that most system level IO in v3 uses bytes not strings. You probably need to convert the bytes to a string before looking for blank lines. But that's just a guess. Also did you try using find() or index() rather than in? HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner: Socket object and packets
On 05Dec2015 13:21, Marc Eymard wrote: Hi tutor, I am trying to locate the first blank line in the first received packet when pinging an internet server using a socket object. First up: everything ALan already said. Next: Note that the HTTP response need not all be in a single packet, though that is not your problem. My assumption is there will be a mandatory blank line right after the http headers in accordance with the http protocol. There certainly should be. Consider the following: import socket mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect( ('www.py4inf.com/code/', 80) ) mysock.send('GET http://www.py4inf.com/code/' + ' HTTP/1.0\n\n') data_str = mysock.recv(700) My question: Why is the following statement False when there is an actual blank line in the received packet: '\n\n' in data 1: You will be getting bytes backup from the server (obviously so in Python 3 and implicitly in Python 2). 2: The HTTP protocol, like most internet text protocols, ends lines with the bytes '\r\n', not just '\n'. Therefore you should expect the bytes '\r\n\r\n' in the response data. However, you should have discovered this already by doing some debugging. Since you're clearly not getting the response you expected, the very first step on your part should be to print our the received data, for example by adding: print(repr(data_str)) after your recv() call. Then you could inspect the received data and probably have seen the '\r' characters. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor