简单 demo

Server.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import socket

s = socket.socket()

host = socket.gethostname()
port = 1234
s.bind((host, port))

s.listen(5) # 最大等待连接数
while True:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()

Client.py

1
2
3
4
5
6
7
8
9
import socket

s = socket.socket()

host = socket.gethostname()
port = 1234

s.connect((host, port))
print s.recv(1024)