client & server

 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
#server:
import socket
sock = socket.socket()
sock.bind(('', 2345))
sock.listen(1)
conn, addr = sock.accept()
print 'successfully connected', addr
my_dict = dict()
my_dict['Brasil'] = 1
my_dict['German'] = 7
while True:
data = conn.recv(1024)
if data == 'exit':
sock.send('bye')
break
elif data:
if my_dict[data]:
conn.send(str(my_dict[data]))
else:
conn.send('no such element')
conn.close()
#client:
import socket
sock = socket.socket()
sock.connect(('localhost', 2345))
sock.send('German')
data = sock.recv(1024)
sock.close()
print data