Created
May 30, 2017 21:11
-
-
Save orsenthil/ff3c3da4664bc116ecfe409cf2c0498f 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
import socket | |
import threading | |
class ThreadedServer(object): | |
def __init__(self, host, port): | |
self.host = host | |
self.port = port | |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
self.sock.bind((self.host, self.port)) | |
def listen(self): | |
self.sock.listen(5) | |
while True: | |
client, address = self.sock.accept() | |
client.settimeout(60) | |
threading.Thread(target = self.listenToClient,args = (client,address)).start() | |
def listenToClient(self, client, address): | |
size = 1024 | |
print "Client connected." | |
while True: | |
try: | |
data = client.recv(size) | |
if data: | |
# Set the response to echo back the recieved data | |
response = data | |
client.send(response) | |
else: | |
raise socket.error('Client disconnected') | |
except: | |
client.close() | |
return False | |
if __name__ == "__main__": | |
port_num = input("Port? ") | |
ThreadedServer('',port_num).listen() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment