首页 技术 正文
技术 2022年11月14日
0 收藏 466 点赞 3,086 浏览 3142 个字

server coding:

 #!/usr/bin/python
# -*- coding: utf-8 -*- import select
import socket
import sys
import Queue # Create a TCP/IP socket
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.setblocking(False) # Bind the socket to the port
server_address = ('localhost',1000)
print >>sys.stderr, 'starting up on %s port %s'%server_address
server.bind(server_address) # Listen for incoming connections
server.listen(5) # Sockets from which we expect to read
inputs = [server] # Sockets to which we expect to write
outputs = [] # Outgoing message queues (socket:Queue)
message_queues = {} while True:
# Wait for at least one of the sockets to be ready for processing
print >>sys.stderr, '\nwaiting for the next event'
#分别筛选出可接收消息的sockets、等待发送消息的sockets、和中途出错的sockets。
#其中exceptional为了把所有可接收消息的sockets都遍历到,所以要从inputs列表中读取。
readable, writable, exceptional = select.select(inputs,outputs,inputs) # Handle inputs
for s in readable:
#第一种情况,创建一个待接收消息的socket放入inputs
if s is server:
# A "readable" server socket is ready to accept a connection
connection, client_address = s.accept()
print >>sys.stderr, 'new connection from', client_address
connection.setblocking(0)
inputs.append(connection) # Give the connection a queue for data we want to send
message_queues[connection] = Queue.Queue() #第二种情况,在后续循环中,已经添加到inputs中的sockets已经不是readable了,所以
#要进行接收消息,消息存在message_queues中,并把该socket添加到outputs。
else:
data = s.recv(1024)
if data:
# A readable client socket has data
print >>sys.stderr, 'receieved "%s" from %s'%(data.upper(),s.getpeername())
message_queues[s].put(data)
# Add output channel for response,添加到待发送消息列表
if s not in outputs:
outputs.append(s) #第三种情况就是这个客户端已经断开了,
#所以你再通过recv()接收到的数据就为空了,
#所以这个时候你就可以把这个跟客户端的连接关闭了。
else:
print >>sys.stderr, 'closing',client_address,'after reading no data'
if s in outputs:
outputs.remove(s)#既然客户端都断开了,我就不用再给它返回数据了,所以这时候如果这个客户端的连接对象还在outputs列表中,就把它删掉
inputs.remove(s)#inputs中也删除掉
s.close()#把这个连接关闭掉 # Remove message queue
del message_queues[s] # Handle outputs
for s2 in writable:
try:
next_msg = message_queues[s2].get_nowait()
#没有消息了
except Queue.Empty:
# No messages waiting so stop checking for writability.
print >>sys.stderr,'output queue for',s2.getpeername(),'is empty'
outputs.remove(s2)
#发送消息
else:
print >>sys.stderr, 'sending "%s" to %s'%(next_msg,s2.getpeername())
s2.send(next_msg) # Handle "exceptional conditions"
for s3 in exceptional:
print >>sys.stderr, 'handling exceptional condition for ',s3.getpeername()
# Stop listening for input on the connection
inputs.remove(s3)
if s3 in outputs:
outputs.remove(s3)
s3.close() #Remove message queue
del message_queues[s3]

client coding

 # -*- coding: utf-8 -*-
"""
Created on Sun Oct 23 14:49:20 2016 @author: fuzzier
""" import sys
import socket messages = ['This is the message.',
'It will be sent',
'in parts.'
]
server_address = ('localhost',1000) # Create a TCP/IP socket,创建两个客户端
socks = [socket.socket(socket.AF_INET,socket.SOCK_STREAM),
socket.socket(socket.AF_INET,socket.SOCK_STREAM)
] # Connect the socket to the port where the server is listening
print >>sys.stderr, 'connecting to %s port %s'%server_address
for s in socks:
s.connect(server_address) for message in messages: # Send messages on both sockets
for s2 in socks:
print >>sys.stderr, '%s:sending "%s"'%(s2.getsockname(),message)
s2.send(message) #Read responses on both sockets
for s3 in socks:
data = s3.recv(1024)
print >>sys.stderr, '%s: received "%s"' % (s3.getsockname(), data.upper())
if not data:
print >>sys.stderr,'closing socket',s3.getsockname()
s3.close()

原理图:

异步select

运行结果:

异步select

异步select

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294