Created
August 23, 2012 02:52
-
-
Save mouseroot/3431634 to your computer and use it in GitHub Desktop.
multithreaded echo server
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
from threading import Thread | |
import socket | |
class clientThread(Thread): | |
def __init__(self,sock): | |
Thread.__init__(self) | |
self.socket = sock | |
self.running = 1 | |
def run(self): | |
while self.running == 1: | |
try: | |
msg = self.socket.recv(1024).decode() | |
print(msg) | |
self.socket.send(msg.encode()) | |
except socket.error: | |
print("Dissconnected!") | |
self.running = 0 | |
self.socket.close() | |
def main(): | |
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
s.bind((socket.gethostname(),1337)) | |
s.listen(3) | |
print("Listening for 3 clients") | |
while 1: | |
cli,addrinfo=s.accept() | |
t = clientThread(cli) | |
t.start() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment