zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python里使用select模块

Python模块 SELECT 使用
2023-09-14 09:10:35 时间
这个模块允许你检查一个或多个socket接收数据,或者连接进来,同时也可以检查文件管道等等。
使用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