1、socket服务端
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
|
import socket
ip_port = ( '192.168.89.101' , 9999 )
sk = socket.socket()
sk.bind(ip_port) sk.listen( 5 )
def show(vals):
if vals = = "aaaa" :
return "i love dengwenqing"
else :
return "not found data"
while True :
print "server waiting..."
conn,addr = sk.accept()
client_data = conn.recv( 1024 )
data = show(client_data) #将客户端传来的值作为参数调用show()函数
conn.sendall(data) #将函数show()的处理结果发送回给客户端
conn.close()
|
2、socket客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import socket
server_ip_port = ( '192.168.89.101' , 9999 )
sk = socket.socket()
sk.connect(server_ip_port) sk.sendall( 'aaaaaa' ) #向服务端发送数据
server_reply = sk.recv( 1024 ) #接受服务端的返回结果
print server_reply
sk.close()
|
本文转自 TtrToby 51CTO博客,原文链接:http://blog.51cto.com/freshair/1890405