Created
December 16, 2013 05:06
-
-
Save studiawan/7982575 to your computer and use it in GitHub Desktop.
Simple HTTP client using socket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
Why is
while True:
needed when the socket receives packets?