Server:
# server import socket address = ('127.0.0.1', 31500) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # s = socket.socket() s.bind(address) s.listen(5) ss, addr = s.accept() print 'got connected from',addr ss.send('byebye') ra = ss.recv(512) print ra ss.close() s.close()
Client:
# client import socket address = ('127.0.0.1', 31500) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(address) data = s.recv(512) print 'the data received is',data s.send('hihi') s.close()
运行结果:
server
[work@db-testing-com06-vm3.db01.baidu.com python]$ python server.py
got connected from ('127.0.0.1', 47872)
hihi
client
[work@db-testing-com06 -vm3.db01.baidu.com python]$ python client.py
the data received is byebye
==============================================================================
参考资料:http://www.cppblog.com/lai3d/archive/2008/02/1 9/42919.html
一个简单的服务器和客户端通信的例子
服务器:
import socket s=socket.socket() s.bind(('xxx.xxx.xxx.xxx',xxxx)) #ip地址和端口号 s.listen(5) cs,address = s.accept() print 'got connected from',address cs.send('byebye') ra=cs.recv(512) print ra cs.close()
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索python
, socket
, send recv
, print
, close
, address
Connected
s7 tcp ip socket通讯、tcp socket通讯流程、python tcp socket、python3 socket tcp、python socket 通讯,以便于您获取更多的相关知识。