我正在用python3.3编写一个http服务器,只是为了学习如何做这种事情。在解析请求的函数中,我想使用fcntl.ioctl来获取我可以在套接字中读取的字节数,并且我只在检查kqueue的结果中看到kevent时才这样做,该队列表示套接字上有要读取的内容。但是每当我试图调用fcntl.ioctl,我得到OSError: [Errno 14] Bad address。我做错什么了?而且,这似乎发生在第一个电话。以下是相关代码:def client_thread(kq, client_socket, methods):

while True:

events = kq.control([], 2, POLLTIME) #we pass an empty list of changes, because we don't have any changes to make to the events we are interested in.

#we want a list that is at most two long. We listen for POLLTIME seconds.

for event in events:

if event != KILL_KEV: #there are only two events in our kqueue

handle_client(client_socket, methods)

else: #KILL_SOCK has a connection

break

client_socket.close()

client_socket.shutdown()

def handle_client(client_socket, methods):

request = parse_request(client_socket) #parse the request data in the client socket

handlers = methods[request["request"]["method"]] #retrieve the appropriate list of handlers from the methods dict

for path_match_pred, handler_func in handlers:

if path_match_pred(path): #if the path matches whatever path predicate you've created...

break

response = handler_func(request) #... then call the appropriate handler function to handle the request

send_response(client_socket, response) #and finally, send the response.

def parse_request(client_socket):

"""Returns the request data, parsed into a dictionary like this:

{

"request": {

"method": method,

"path": path,

"version": HTTP version

},

"headers": header dictionary,

"body": body data as a string

}

This should only be called if the client socket is ready for reading!

"""

client_fd = client_socket.fileno() #get the file descriptor for the socket

bytes_in_socket = 0

fcntl.ioctl(client_fd, termios.FIONREAD, bytes_in_socket) #count the bytes in it

#^^^^^^^^^THIS IS WHERE IT BREAKS

print(bytes_in_socket, "bytes in socket")

msg = bytearray() #make empty byte array

while bytes_in_socket:

msg.extend(client_socket.recv(bytes_in_socket)) #read the bytes we counted earlier

fcntl.ioctl(client_fd, termios.FIONREAD, bytes_in_socket) #check for more bytes

print(bytes_in_socket, "bytes left to read")

python ioctl_我使用ioctl正确吗?相关推荐

  1. python中基本程序结构_关于Python 程序格式框架的描述,正确的是( )

    [判断题]元组的元素是可读的,可以对元组进行更新.增加.删除操作. [多选题]哪些选项关于循环结构的描述是正确的( ) [单选题]以下可以终结一个循环的是 . [判断题]对于大量列表的连接,exten ...

  2. 这样安装python库才是最正确的哦_这样安装 Python 库才是最正确的哦~

    原标题:这样安装 Python 库才是最正确的哦~ 分享一篇之前的文章,里面的这个Python知识点,可能很多人都还不清楚. 平常我都是直接执行 pip install安装的第三方库,很多教程也是这么 ...

  3. 下列关于python运算符的使用描述正确的是_以下关于 Python 字符串的描述中,正确的是( )...

    [单选题]3*1**3 表达式输出结果为________ [多选题]以下的布尔代数运算正确的是( ) [单选题]下列________不是Python的数据类型. [单选题]下列不是Python语言关键 ...

  4. 【Python】NumPy 中 ravel() 正确打开方式

    [Python]NumPy 中 ravel() 正确打开方式 最近在看书时,对numpy的ravel()的有些疑惑,书上这样写到"如果结果中的值在原始数组中是连续的,则ravel 不会生成底 ...

  5. python读取csv文件路径正确但报错FileNotFoundError: [Errno 2] No such file or directory

    项目场景: python读取csv文件路径正确但报错FileNotFoundError: [Errno 2] No such file or directory 问题描述: #储存调用表格.数据 #调 ...

  6. 关于python语言中缩进的说法_以下关于Python语言中“缩进”说法正确的是:

    [多选题]From the perspective of the production enterprise (the content is more extensive), any differen ...

  7. python list遍历删除,对python list 遍历删除的正确方法详解

    在遍历list的时候,删除符合条件的数据,可是总是报异常,代码如下: num_list = [1, 2, 3, 4, 5] print(num_list) for i in range(len(num ...

  8. 关于python语言、下列说法不正确的是-以下关于Python语言中“缩进”说法正确的是:...

    问题:以下关于Python语言中"缩进"说法正确的是: 更多相关问题 在金融市场中,既是重要的资金需求者和供给者,又是金融衍生品市场上重要的套期保值主体 非现场监管是监管当局分析银 ...

  9. 以下python注释代码不正确的有_以下Python注释代码,不正确的是()

    以下Python注释代码,不正确的是() 答://Python注释代码 初偏心会使轴心受压构件的稳定承载力降低.(?) 答:对 外国现代军事思想有___ 答:"总体战"理论 &qu ...

最新文章

  1. LeetCode Same Tree
  2. JProfiler 5.1.4的使用方法
  3. android Lint优化代码
  4. 汽车维修行业呼吁大学生加入修车行列
  5. DNN:LSTM的前向计算和参数训练
  6. P6378 [PA2010] Riddle 2-sat + 前缀和优化建图
  7. ubantu获取信息_Ubuntu 下查看CPU 信息命令
  8. 吃鱼可以不挑刺了?华中农业大学发现鳊鱼肌间刺表达基因,可培育“无刺鱼”...
  9. 网络规划设计师考试经验分享
  10. 想学人工智能从哪入手?
  11. 解决谷歌浏览器重复上传同一文件失败的问题
  12. 正则表达+验证 [记录]
  13. QQ机器人实现RSS订阅(github项目)
  14. (超详细)2022年最新版java 8( jdk1.8u321)安装教程
  15. 生信技能树课程记录笔记(五)20220528
  16. Android毕业实践报告,毕业实习报告范文
  17. 软件测试员最核心的竞争力究竟是什么
  18. 服务器能ping通,但是无法ssh
  19. R | failed to load cairo DLL
  20. Twebbrowser从内存中加载页面

热门文章

  1. 2023 0727 0727
  2. 初始化仓库git init和git init --bare的区别和使用场景
  3. Fiddler 工作原理及界面简介
  4. jpa 利用JpaSpecificationExecutor做复杂多表联合查询
  5. 通讯录排序(结构嵌套)
  6. 制作Windows的ico图标
  7. C#中Button.DialogResult属性
  8. .online24files@airmail.cc勒索病毒数据恢复(Scarab系列)
  9. C#实现ActiveX控件开发与部署---开发实现篇
  10. 趋势预测:2021年五大流行的编程语言