Dr Chuck - Legacy Python Code Failure

Dr. Chucks code is as follows…

import socket

mysock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((‘data.pr4e.org’, 80))
cmd='GET http://data.pr4e.org/romeo.txt HTTP/1.0\n\n’.encode()
mysock.send(cmd)

while True:
data = mysock.recv(512)
if (len(data) < 1):
break
print(data.decode())
mysock.close()

I believe the code needs updating to HTTP/2, what would be the correct code as I keep getting the error… ‘This server did not understand the request’.
Also does the code need to be sock4/sock5 compliant?

I ran into this problem a while ago, as there are missing carriage returns I think.
Try this code instead:

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
    print(data.decode(),end='')

1 Like