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() |
This listens for connections comming in and for each client that connects
it spawns a thread for each client by passing in the socket that is created in the while loop to the clientThread class (that has a base Class of Thread)
No comments:
Post a Comment