-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookieServer.py
76 lines (63 loc) · 1.88 KB
/
cookieServer.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
import socket
host = ''
port = 5560
#Definindo um valor de informacao de teste para ser transferido.
storedValue = "Hello Wolrd"
#Definindo a configuracao de conexao
def setupServer():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket created.")
try:
s.bind((host, port))
except socket.error as msg:
print(msg)
print("Socket bind complete.")
return s
def setupConnection():
s.listen(1) #Permite uma conexao por vez.
conn, address = s.accept()
print("Connected to: "+ addres[0] + ":" + str(address[1]))
return conn
def GET():
reply = storesValue
return reply
def REPEAT(dataMessage):
reply = dataMessage[1]
return reply
def dataTransfer(conn):
#Um loop que recebe e envia mensagens
while True:
#receber os dados
data = conn.recv(1024)
#Python 3 necessita da diferenciacao de dados entre bytes e strings
#Significa que para receber temos que decodificar
#Para enviar temos que codificar
data = data.decode('utf-8') #utf-8 e string
#Dividir os dados de forma que separe o comando do resto dos dadoss
dataMessage = data.split(' ', 1)
command = dataMessage[0]
if command == 'GET':
reply = GET()
elif command == 'REPEAT':
reply = REPEAT(dataMessage)
elif command == 'EXIT':
print("Our cliente hates us :(")
break
elif command == "KILL":
print("Our server is shutting down.")
s.close()
break
else:
reply = "Unkown Command"
#enviar dados ao cliente
conn.sendall(str.encode(reply))
print("Data has been sent!")
conn.close()
s = setupServer()
while True:
try:
conn = setupConnection()
dataTransfer(conn)
except:
print("Error!")
break