1、socket服务端
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客户端
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()
转载于:https://blog.51cto.com/freshair/1890405