-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
53 lines (47 loc) · 1.28 KB
/
client.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
#Parametros do Cliente : Porta Local, Endereco de Ip do servidor, porta do servidor
import select
import socket
import struct
import sys
from sys import stdin
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def main(argv):
HOST = None
PORT = None
LOCAL_PORT = None
# Deve receber os 3 parametros para iniciar conexao
if len(argv) >= 3:
LOCAL_PORT = argv[0]
HOST = argv[1] # Endereco IP do Servidor
PORT = argv[2] # Porta em que o Servidor esta
server.bind(('',int(LOCAL_PORT)))
input = [server,stdin]
running = 1
# send_message(HOST, PORT, TEXT)
while running:
inputready,outputready,exceptready = select.select(input,[],[],)
for s in inputready: # PARA LINUX
if s == server:
data = s.recv(500).decode('ascii')
print(data)
elif s == stdin:
# handle standard input
TEXT = stdin.readline()
TEXT = TEXT.replace('\n', '')
send_message(HOST, PORT, TEXT)
else:
# handle all other sockets
print("outros sockets")
data = s.recv(500).decode('ascii')
print(data)
if data:
s.send(data)
else:
s.close()
input.remove(s)
server.close()
def send_message(HOST, PORT, message):
dest = (HOST, int(PORT))
server.sendto(message.encode('ascii'), dest)
if __name__ == "__main__":
main(sys.argv[1:])