当前位置: 编程技术>python
python实现socket客户端和服务端简单示例
来源: 互联网 发布时间:2014-10-04
本文导语: 代码如下:import socket#socket通信客户端def client(): mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM) mysocket.connect(('127.0.0.1',8000)) mysocket.send('hello') while 1: data=mysocket.recv(1024) if data: pr...
代码如下:
import socket
#socket通信客户端
def client():
mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
mysocket.connect(('127.0.0.1',8000))
mysocket.send('hello')
while 1:
data=mysocket.recv(1024)
if data:
print data
else:
break
mysocket.close()
#服务器端
def server():
ser=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ser.bind(('127.0.0.1',8000))
ser.listen(5)
while 1:
client,addr=ser.accept()
print 'accept %s connect'%(addr,)
data=client.recv(1024)
print data
client.send('get')
client.close()