little help in homework required
Chris Angelico
rosuav at gmail.com
Fri Jun 16 06:21:38 EDT 2017
On Fri, Jun 16, 2017 at 7:06 PM, JAIDIP patel <jaidip.81 at gmail.com> wrote:
> The code I wrote:
>
> import socket
>
> mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> mysock.connect(('data.pr4e.org', 80))
> mysock.send('GET http://data.pr4e.org/intro-short.txt HTTP/1.0\n\n')
> while True:
> data =x.recv(512)
> if ( len(data) < 1 ) :
> break
> print (data)
>
> mysock.close()
Lutz has already suggested one solution, but here's an alternative:
you can use a byte-string literal.
mysock.send(b'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\n\n')
While you're at it, though, I recommend complying with protocol correctly:
mysock.send(b'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\r\n\r\n')
The HTTP specification says that lines should end with CR LF.
You can actually simplify your condition, too:
if not data:
This is considered more "Pythonic" - that is to say, it's more the way
that experienced Python programmers work. Consider it points for
style. :)
Hope that helps!
ChrisA
More information about the Python-list
mailing list