Skip to content

Instantly share code, notes, and snippets.

@ninehills
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninehills/76f8a4dee9a9c79cc498 to your computer and use it in GitHub Desktop.
Save ninehills/76f8a4dee9a9c79cc498 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# a simple multi-threaded TCP Black Hole server
import sys
from socket import *
import threading
import thread
import time
def handler(clientsock,addr):
try:
while 1:
data = clientsock.recv(65535);
if not data:
break
finally:
print 'connection close...'
clientsock.close()
if __name__ == '__main__':
HOST = '0.0.0.0'
PORT = int(sys.argv[1])
BUFSIZ = 65535
ADDR = (HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(5)
while 1:
print 'waiting for connection...'
clientsock, addr = serversock.accept()
print '...connected from: ', addr
thread.start_new_thread(handler, (clientsock, addr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment