-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver2.py
53 lines (35 loc) · 1.03 KB
/
server2.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
import socket
import pickle
import random
sct = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#IP = socket.gethostbyname(socket.gethostname())
IP = "0.0.0.0"
port = 4442
sct.bind((IP, port))
sct.listen(5)
print(f"Server is running {IP} on port {port}")
class Instructions:
url = ""
msg = ""
additional_param = ""
def action(data):
decoded_data = pickle.loads(data)
n = random.randint(-5000,50000)
n = str(n)
decoded_data.msg = "Welcome to givemerandom.com, here is your number " + n
#print("done")
return pickle.dumps(decoded_data.msg)
try:
while True:
clientsocket, address = sct.accept()
print(f"Connection from {address} has been established")
data = clientsocket.recv(1024)
new_data = action(data)
print("Data recived")
clientsocket.sendall(new_data)
clientsocket.shutdown(socket.SHUT_WR)
except KeyboardInterrupt:
print("Shutting down the server")
except Exception as exc:
print("Error ocurred: ", exc)
sct.close()