Server:
[python] view plain copy print?
- import socket
-
- address = ('127.0.0.1', 31500)
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- s.bind(address)
-
- while True:
- data, addr = s.recvfrom(2048)
- if not data:
- print "client has exist"
- break
- print "received:", data, "from", addr
-
- s.close()
Client:
[python] view plain copy print?
- import socket
-
- address = ('127.0.0.1', 31500)
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-
- while True:
- msg = raw_input()
- if not msg:
- break
- s.sendto(msg, address)
-
- s.close()
运行结果:
server
[work@db-testing Python]$ python udp_ss.py
received: aaaaaa from ('127.0.0.1', 10169)
received: 123456 from ('127.0.0.1', 10169)
received: abcdef from ('127.0.0.1', 10169)
client
[work@db-testing python]$ python udp_cc.py
aaaaaa
123456
abcdef
[work@db-testing python]$
==========================================
python支持BSD的socket进行网络编程,其API跟C中的大同小异,先看看TCP方式的,说到网络编程,肯定会设计到server和client,分别来看下
TCP方式
server端
server端的socket一般流程是这样:
- 建立一个socket(可以选择socket类型INET,UNIX等,以及连接方式TCP/UDP)
- 使用bind公开一个端口,使得client可以方便连接
- 设置一个listen队列的大小
- 进入一个无限循环,在此无限循环中,使用accept函数来等待客户连接,此函数返回一个新的socket, 对应于客户端的socket,建立通信信道。对socket的处理一般放在外部单独的函数中(并发)
- 通过send()/recv()来对socket进行读写操作
好了,下面看一下例子:
netserver.py
[python] view plain copy print?
-
-
-
- import sys
- reload(sys)
- sys.setdefaultencoding('utf-8')
-
- import socket
-
- class NetServer(object):
- def tcpServer(self):
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.bind(('', 9527))
- sock.listen(5)
-
- while True:
- clientSock, (remoteHost, remotePort) = sock.accept()
- print("[%s:%s] connect" % (remoteHost, remotePort))
-
- revcData = clientSock.recv(1024)
- sendDataLen = clientSock.send("this is send data from server")
- print "revcData: ", revcData
- print "sendDataLen: ", sendDataLen
-
- clientSock.close()
-
- if __name__ == "__main__":
- netServer = NetServer()
- netServer.tcpServer()
client端
- 建立一个新的socket
- 使用connect函数与远程主机获得连接
- 在此socket上进行I/O操作
netclient.py
[python] view plain copy print?
-
-
-
- import sys
- reload(sys)
- sys.setdefaultencoding('utf-8')
-
- import socket
-
- class NetClient(object):
- def tcpclient(self):
- clientSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- clientSock.connect(('localhost', 9527))
-
- sendDataLen = clientSock.send("this is send data from client")
- recvData = clientSock.recv(1024)
- print "sendDataLen: ", sendDataLen
- print "recvData: ", recvData
-
- clientSock.close()
-
- if __name__ == "__main__":
- netClient = NetClient()
- netClient.tcpclient()
运行结果:
1) 先启动 netserver.py, 然后启动 netclient.py,注意client先发送后接收,server是先接收后发送,顺序要交错
2) server 打印结果:
[127.0.0.1:55279] connect
revcData: this is send data from client
sendDataLen: 30
3) client 打印结果:
sendDataLen: 29
recvData: this is send data from server
由代码量可见python之简单,想要将这个例子改造成一个文件传输器,估计只需要增加不到20行代码即可。
==========================================
UDP方式
UDP号称无连接传输,全然没有TCP那么复杂,三次握手,错误重传之类的机制都没有,发的只管发,收得只管收,收到没有?不知道,顺序不对怎么办?不管!就是这样,但是速度就要比TCP高得多了。在对数据帧要求不是很高的地方,这确实是很好用的,比如网络上的视频传输,音频传输等。
server端
- 建立数据报形式的socket
- 公开一个端口,一边客户端连接
- 开始接收数据
udpserver.py
[python] view plain copy print?
-
-
-
- import sys
- reload(sys)
- sys.setdefaultencoding('utf-8')
-
- import socket
-
- class UdpServer(object):
- def tcpServer(self):
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- sock.bind(('', 9527))
-
- while True:
- revcData, (remoteHost, remotePort) = sock.recvfrom(1024)
- print("[%s:%s] connect" % (remoteHost, remotePort))
-
- sendDataLen = sock.sendto("this is send data from server", (remoteHost, remotePort))
- print "revcData: ", revcData
- print "sendDataLen: ", sendDataLen
-
- sock.close()
-
- if __name__ == "__main__":
- udpServer = UdpServer()
- udpServer.tcpServer()
需要注意的是,server中address元组中的引号表示可以接受任何地址来的数据报,TCP例子中的则表示可以接受任意地址发起的连接。
client端
- 新建一个数据报socket
- 收发数据
udpclient.py
[python] view plain copy print?
-
-
-
- import sys
- reload(sys)
- sys.setdefaultencoding('utf-8')
-
- import socket
-
- class UdpClient(object):
- def tcpclient(self):
- clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-
- sendDataLen = clientSock.sendto("this is send data from client", ('localhost', 9527))
- recvData = clientSock.recvfrom(1024)
- print "sendDataLen: ", sendDataLen
- print "recvData: ", recvData
-
- clientSock.close()
-
- if __name__ == "__main__":
- udpClient = UdpClient()
- udpClient.tcpclient()
运行结果:
1) 先启动 udpserver.py,然后启动 udpclient.py,并且保证client先发后收,server先收后发
2) server 结果(client 连续发送了三次请求):
[127.0.0.1:37219] connect
revcData: this is send data from client
sendDataLen: 30
[127.0.0.1:57622] connect
revcData: this is send data from client
sendDataLen: 30
[127.0.0.1:52290] connect
revcData: this is send data from client
sendDataLen: 30
3) client 结果:
sendDataLen: 29
recvData: ('this is send data from server', ('127.0.0.1', 9527))
文中给出的例子已经具有网络程序的基本结构,在实际应用中,应该自行扩展对socket的处理,socket说到底跟文件句柄,文件描述符,管道描述符等都是一个概念,可以对其进行I/O处理,事实上,在UNIX类系统中,对socket的操作跟对文件句柄的操作没有任何形式上的差异。
==========================================