-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
59 lines (42 loc) · 1.28 KB
/
server.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import socket
import pickle
from _thread import *
import threading
#print_lock = threading.Lock()
# thread
def threaded(c):
while True:
# data received from client 1024 byte
data = pickle.loads(c.recv(1024))
if not data:
print('Connection terminated')
# lock released on exit
#print_lock.release()
break
# reverse the given string from client
data = data[::-1]
# send back reversed string to client
c.send(pickle.dumps(data))
# connection closed
c.close()
def Main():
host = ""
port = 7070
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("socket binded to post", port)
# put the socket into listening mode
s.listen(5)
print("server Initiated")
# a forever loop until client wants to exit
while True:
# establish connection with client
c, addr = s.accept()
# lock acquired by client
#print_lock.acquire()
print('Connection from :', addr[0], ':', addr[1])
# Start a new thread and return its identifier
start_new_thread(threaded, (c,))
s.close()
if __name__ == '__main__':
Main()