python里使用select模块
来源:互联网 发布:淘宝韩国女模特 编辑:程序博客网 时间:2024/05/22 03:40
这个模块允许你检查一个或多个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.pyimport selectimport socketimport struct, timePORT = 8037TIME1970 = 2208988800service = 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.pyimport socketimport struct, time# serverHOST = '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 servers = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect((HOST, PORT))# read 4 bytes, and convert to time valuet = s.recv(4)t = struct.unpack("!I", t)[0]t = int(t - TIME1970)s.close()# print resultsprint("server time is", time.ctime(t))print("local clock is", int(time.time()) - t, "seconds off")
Python游戏开发入门
http://edu.csdn.net/course/detail/5690
你也能动手修改C编译器
http://edu.csdn.net/course/detail/5582
纸牌游戏开发
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
阅读全文
0 0
- python里使用select模块
- Python:简单select模块使用
- python里使用asyncore模块
- Python中的select模块如何使用?
- python --select模块
- Python的Select模块
- python使用twisted里log模块操作text记录日志
- python select模块实现并发
- python下的select模块使用 以及epoll与select、poll的区别
- Python里安装NumPy模块
- python里的re模块
- python里的logging模块
- Python里 模块、Package概念
- python select网络编程模块详解
- Python select模块 I/O多路复用
- 在python中编写socket服务端模块(一):使用select
- 在python中编写socket服务端模块(一):使用select
- python 查找模块里的所有函数
- 自定义进度条列表
- BeautifulSoup库之find、findAll和children、descendants
- 服务器网站报错:由于扩展配置问题无法提供您请求的页面,请添加MIME映射,针对mp4,flv文件类型无法打开。
- 网卡配置
- jquery设置控件是否可用,是否可见
- python里使用select模块
- LeetCode--Maximum Subarray
- 转义
- ConstraintLayout 使用初探
- http和https的区别以及超全局变量
- leetcode 27 removeElement
- Java I/O流总结
- 工件调度
- BZOJ2716: [Violet 3]天使玩偶(CDQ分治)