forked from fa-python-network/2_threaded_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
30 lines (25 loc) · 921 Bytes
/
server.py
File metadata and controls
30 lines (25 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import socket
import threading
class ClientThread(threading.Thread):
def __init__(self, clientAddress, clientsocket):
threading.Thread.__init__(self)
self.csocket = clientsocket
def run(self):
print ("Connection from : ", clientAddress)
self.csocket.send(bytes("Hi, This is from Server..", 'utf-8'))
msg = ''
while True:
data = self.csocket.recv(2048)
msg = data.decode()
print ("from client", msg)
self.csocket.send(bytes(msg, 'UTF-8'))
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('localhost', 9999))
print("Server started")
print("Waiting for client request..")
while True:
server.listen(1)
clientsock, clientAddress = server.accept()
newthread = ClientThread(clientAddress, clientsock)
newthread.start()