-
Notifications
You must be signed in to change notification settings - Fork 1
/
count_server.py
99 lines (87 loc) · 2.6 KB
/
count_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import sys
import socket
import threading
import logging
contador = 0
def make_tiny():
logging.info(f'-- Increased request counter')
global contador
contador += 1
return contador
def start(socket):
logging.basicConfig(filename="Client.log",
filemode="a",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt='%m/%d/%Y %I:%M:%S %p'
)
try:
while True:
data = socket.recv(1024)
logging.info(f'-- Recieving count notice')
if not data:
break
request = str(make_tiny())
print("Peticiones : ", request)
socket.sendall(request.encode('ascii'))
except KeyboardInterrupt:
print("Cerrando servidor")
socket.close()
logging.info(f'KEYBOARD INTERRUPT SHUTDOWN')
finally:
socket.close()
logging.info(f'TRANSACTION FINALIZATION')
def return_count_short():
logging.basicConfig(filename="Client.log",
filemode="a",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt='%m/%d/%Y %I:%M:%S %p'
)
try:
while True:
logging.info(f'-- Returning request count')
data = socket.recv(1024)
if not data:
break
request = str(make_tiny())
#print("Peticiones : ",request)
socket.sendall(request.encode('ascii'))
except KeyboardInterrupt:
#print("Cerrando servidor")
socket.close()
logging.info(f'KEYBOARD INTERRUPT SHUTDOWN')
finally:
socket.close()
logging.info(f'TRANSACTION FINALIZATION')
def main():
global THIS_HOST
THIS_HOST = ""
global THIS_PORT
THIS_PORT = 3000
print("Creating Socket")
logging.info(f'TRANSACTION BEGINS')
s = socket.socket()
s.bind((THIS_HOST, THIS_PORT))
try:
s.listen(5)
while True:
conn, addr = s.accept()
logging.info(f'-- Accept socket connection')
#print("Connected to : ", addr)
logging.info(f'CONNECTION ESTABLISHED IN {addr}')
t = threading.Thread(target=start, args=(conn,))
t.start()
logging.info(f'-- Open thread')
except KeyboardInterrupt:
print("Error en main")
finally:
s.close()
if __name__ == "__main__":
main()
logging.basicConfig(filename="CountServer.log",
filemode="a",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt='%m/%d/%Y %I:%M:%S %p'
)