-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq1_cliente.py
More file actions
73 lines (55 loc) · 1.81 KB
/
Copy pathq1_cliente.py
File metadata and controls
73 lines (55 loc) · 1.81 KB
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
import socket
import http.client, urllib.parse, json
def get_protocol(arq):
f = open(arq, "r")
protocolo = f.readline()
f.close()
return protocolo
def echo(protocolo):
if protocolo == "UDP\n":
HOST = '127.0.0.1'
PORT = 5000
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dest = (HOST, PORT)
print ('Para sair use CTRL+X\n')
msg = input()
while msg != '\x18':
udp.sendto (msg.encode('utf-8'), dest)
resp, server = udp.recvfrom(1024)
print (server, resp.decode('utf-8'))
msg = input()
udp.close()
elif protocolo == "TCP\n":
HOST = '127.0.0.1'
PORT = 5001
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dest = (HOST, PORT)
tcp.connect(dest)
print ('Para sair use CTRL+X\n')
msg = input()
while msg != '\x18':
tcp.send(msg.encode('utf-8'))
resp = tcp.recv(1024)
print (dest, resp.decode('utf-8'))
msg = input()
tcp.close()
elif protocolo == 'HTTP\n':
print ('Para sair use CTRL+X\n')
msg = input()
while msg != '\x18':
params = json.dumps({'msg': msg})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = http.client.HTTPConnection("127.0.0.1", 8000)
conn.request("POST", "", params, headers)
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
print (data.decode())
msg = input()
def main():
protocol = get_protocol('q1_protocolo.txt')
print ("protocolo:", protocol)
echo(protocol)
if __name__ == '__main__':
main()