Skip to content

Instantly share code, notes, and snippets.

@amuraru
Last active December 25, 2015 23:59
Show Gist options
  • Save amuraru/7060751 to your computer and use it in GitHub Desktop.
Save amuraru/7060751 to your computer and use it in GitHub Desktop.
from socket import socket
import sys
def readlines(sock, recv_buffer=4096, delim='\n', wait=False):
buffer = ''
data = True
while data:
data = sock.recv(recv_buffer)
buffer += data
while buffer.find(delim) != -1:
line, buffer = buffer.split('\n', 1)
yield line
if not wait: break
return
sock = socket()
host = sys.argv[1]
port = int(sys.argv[2])
print host
print port
sock.connect((host, port))
sock.sendall('GET / HTTP/1.1\r\nHOST: %s\r\nConnection: keep-alive\r\n\r\n' % host)
for line in readlines(sock):
print line
sock.sendall('GET / HTTP/1.1\r\nHOST: %s\r\nConnection: keep-alive\r\n\r\n' % host)
for line in readlines(sock):
print line
sock.sendall('GET / HTTP/1.1\r\nHOST: %s\r\nConnection: keep-alive\r\n\r\n' % host)
for line in readlines(sock,wait=True):
print line
sock.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment