Created
February 6, 2015 10:39
-
-
Save msulima/83327fda9fdc897b2445 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import socket, threading, sys | |
class ClientThread(threading.Thread): | |
def __init__(self, ip, port, socket, thread_id): | |
threading.Thread.__init__(self) | |
self.ip = ip | |
self.port = port | |
self.socket = socket | |
self.thread_id = thread_id | |
print self.tid() + "New thread started for "+ip+":"+str(port) | |
def run(self): | |
print self.tid() + "Connection from : "+ip+":"+str(port) | |
clientsock.send("\nWelcome to the server, " + self.tid() + "\n") | |
data = "dummydata" | |
while len(data): | |
data = clientsock.recv(2048) | |
print self.tid() + "Client sent: " + data, | |
clientsock.send("You sent me: "+data) | |
print self.tid() + "Client disconnected..." | |
def tid(self): | |
return "[" + str(self.thread_id) + "] " | |
host = "0.0.0.0" | |
port = 2003 | |
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
tcpsock.bind((host,port)) | |
tcpsock.listen(5) | |
lastid = 1 | |
try: | |
while True: | |
print "listening..." | |
(clientsock, (ip, port)) = tcpsock.accept() | |
ct = ClientThread(ip, port, clientsock, lastid) | |
ct.daemon=True | |
ct.start() | |
lastid = lastid+1 | |
except: | |
tcpsock.close() | |
sys.exit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment