Skip to content

Instantly share code, notes, and snippets.

@snullp
Last active August 29, 2015 14:08
Show Gist options
  • Save snullp/cd50728dee1d6811e188 to your computer and use it in GitHub Desktop.
Save snullp/cd50728dee1d6811e188 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import socket, time, threading
TCP_IP = '0.0.0.0'
TCP_PORT = 6789
BUFFER_SIZE = 1024
REPEAT = 100
content = "<test>\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
def worker(conn, addr):
print 'Connection address:', addr
data = conn.recv(BUFFER_SIZE)
print "received data:", data
head = "HTTP/1.1 200 OK\r\nContent-Length: "+str(len(content)*REPEAT)+"\r\nLast-Modified: Mon, 04 Oct 2004 15:04:06 GMT\r\nDate: "+time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())+"\r\nExpires: "+time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time()+3600))+"\r\nAccept-Ranges: bytes\r\n\r\n"
conn.send(head)
loop=0
while loop<REPEAT:
p=0
while p<len(content):
conn.send(content[p])
p=p+1
time.sleep(0.2)
loop=loop+1
print addr[1], loop*len(content)
conn.close()
while True:
conn, addr = s.accept()
t = threading.Thread(target=worker, args=(conn,addr))
t.daemon = True
t.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment