发布时间:2019-09-26 12:29:09编辑:auto阅读(2125)
Python socket 使用方法如下
socket = socket.socket(family, type[, protocal])
服务器端:
import socket
import os
if __name__ == '__main__':
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
if os.path.exists("/tmp/test.sock"):
os.unlink("/tmp/test.sock")
server.bind("/tmp/test.sock")
server.listen(0)
while True:
connection, address = server.accept()
connection.send("test: %s"% connection.recv(1024))
connection.close()
客户端:
import socket
import os
if __name__ == '__main__':
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.connect("/tmp/test.sock")
instr = raw_input()
client.send(instr)
print client.recv(1024)
client.close()
服务器端:
import socket
import os
if __name__ == '__main__':
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 8888))
server.listen(0)
while True:
connection, address = server.accept()
connection.send("test: %s"% connection.recv(1024))
connection.close()
客户端:
import socket
import os
if __name__ == '__main__':
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("localhost", 8888))
instr = raw_input()
client.send(instr)
print client.recv(1024)
client.close()
参考文档
1. http://yangrong.blog.51cto.com/6945369/1339593
2. http://blog.csdn.net/yuxi2046/article/details/6996105
3. http://blog.csdn.net/sealyao/article/details/4704876
4. http://baiying.blog.51cto.com/1068039/1112997
上一篇: python 三行代码实现快速排序
下一篇: python清除函数占用的内存
47750
46254
37136
34643
29233
25893
24762
19866
19428
17917
5720°
6324°
5843°
5892°
6993°
5831°
5851°
6365°
6319°
7684°