-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
47 lines (35 loc) · 1011 Bytes
/
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
import sys
import socket
# Host
HOST = ''
# Porta
PORT = int(sys.argv[1])
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
orig = (HOST, PORT)
server.bind(orig)
server.listen(1)
print 'Ouvindo...\n'
conection, cliente = server.accept()
print 'Concetado por', cliente
# Recebendo mensagem do cliente
msg = conection.recv(1024)
print 'Mensagem recebida: ', msg.decode("utf-8")
# Verifica se a string eh numerica
if unicode(msg, 'utf-8').isnumeric():
msg = int(msg) + 1
msg = str(msg)
# Verifica se eh caracter maiusculo
elif len(msg) == 1 and unicode(msg, 'utf-8').isupper():
msg = msg.lower()
# Verifica se eh caracter minusculo
elif len(msg) == 1 and unicode(msg, 'utf-8').islower():
msg = msg.upper()
else:
# Inverte string
msg = msg[::-1]
# Enviando mensagem para o cliente
conection.send(msg.encode("utf-8"))
conection.shutdown(socket.SHUT_RDWR)
conection.close()
print 'Conexao com o cliente ', cliente, ' finalizada'