Skip to content

Instantly share code, notes, and snippets.

@studiawan
Created December 16, 2013 05:06
Show Gist options
  • Save studiawan/7982575 to your computer and use it in GitHub Desktop.
Save studiawan/7982575 to your computer and use it in GitHub Desktop.
Simple HTTP client using socket
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('www.python.org', 80)
client_socket.connect(server_address)
request_header = 'GET / HTTP/1.0\r\nHost: www.python.org\r\n\r\n'
client_socket.send(request_header)
response = ''
while True:
recv = client_socket.recv(1024)
if not recv:
break
response += recv
print response
client_socket.close()
@local-interloper
Copy link

Hi, @flowingwings. recv reads n amount of bytes from the connection. In order to make sure we get the whole message (that can, of course, be larger than 1024B) we call it until we get nothing which means we got the whole mesage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment