python里使用select模块
2023-09-14 09:10:35 时间
这个模块允许你检查一个或多个socket接收数据,或者连接进来,同时也可以检查文件管道等等。
使用select函数可以检查多个socket是否可读、可写、出错等状态。
一个socket是否可读,主要有三种情况:1)新的连接进来 2)有数据可以接收 3)socket关闭、出错
输出结果如下:
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
使用select函数可以检查多个socket是否可读、可写、出错等状态。
一个socket是否可读,主要有三种情况:1)新的连接进来 2)有数据可以接收 3)socket关闭、出错
一个socket是否可写,主要有两种情况:1)一个连接调用connect之后连接成功 2)数据可以发送
例子如下:
# File: select-example-1.py
import select
import socket
import struct, time
PORT = 8037
TIME1970 = 2208988800
service = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
service.bind(("", PORT))
service.listen(1)
print("listening on port", PORT)
while True:
is_readable = [service]
is_writable = []
is_error = []
r, w, e = select.select(is_readable, is_writable, is_error, 1.0)
if r:
channel, info = service.accept()
print("connection from", info)
t = int(time.time()) + TIME1970
t = struct.pack("!I", t)
channel.send(t) # send timestamp channel.close() # disconnect
else:
print("still waiting")
输出结果如下:
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
connection from ('127.0.0.1', 53313)
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
still waiting
测试的客户端例子:
# File: socket-example-1.py
import socket
import struct, time
# server
HOST = '127.0.0.1'#"time.nist.gov"
PORT = 8037
# reference time (in seconds since 1900-01-01 00:00:00)
TIME1970 = 2208988800 # 1970-01-01 00:00:00
# connect to server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
# read 4 bytes, and convert to time value
t = s.recv(4)
t = struct.unpack("!I", t)[0]
t = int(t - TIME1970)
s.close()
# print results
print("server time is", time.ctime(t))
print("local clock is", int(time.time()) - t, "seconds off")
Python游戏开发入门
你也能动手修改C编译器
纸牌游戏开发
http://edu.csdn.net/course/detail/5538
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
RPG游戏从入门到精通
http://edu.csdn.net/course/detail/5246
WiX安装工具的使用
http://edu.csdn.net/course/detail/5207
俄罗斯方块游戏开发
http://edu.csdn.net/course/detail/5110
boost库入门基础
http://edu.csdn.net/course/detail/5029
Arduino入门基础
http://edu.csdn.net/course/detail/4931
Unity5.x游戏基础入门
http://edu.csdn.net/course/detail/4810
TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
TensorFlow入门基本教程
http://edu.csdn.net/course/detail/4369
C++标准模板库从入门到精通
http://edu.csdn.net/course/detail/3324
跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901
跟老菜鸟学python
http://edu.csdn.net/course/detail/2592
在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672
相关文章
- python 高精度浮点数计算模块decimal
- Python:itertools模块(转)
- python-django框架-电商项目-用户模块开发_20191117
- python入门-分类和回归各种初级算法
- Python下的XML-RPC客户端和服务端实现(基于xmlrpclib SimpleXMLRPCServer 模块)
- 【python cookbook】【字符串与文本】9.将Unicode文本统一表示为规范形式
- 转-[Python 学习]2.5版yield之学习心得
- python paramiko模块学习分享
- Python-datetime模块详解(未完,待续)
- 【代码】python paramiko模块代码示例:远程执行命令及上传和下载
- python pyyaml模块使用示例:读取yaml文件内容
- 华为OD机试 - 找到比自己强的人数(Java & JS & Python)
- python 读取音频文件(mp3,wav)时间的两种方法
- 手把手教你用python几行代码打造人工智能对话机器人,还说说话!
- Python实现SMA黏菌优化算法优化支持向量机分类模型(SVC算法)项目实战
- python爬虫模块之HTML下载模块
- 【LeetCode Python实现】737. 句子相似性 II(中等)
- Python编程:动态导入模块
- python运维开发常用模块(6)发送电子邮件模块smtplib
- python里怎么样实现模块按需加载
- Python Requests模块session的使用建议
- Python基础=== Tkinter Grid布局管理器详解
- python的包和模块