python ioctl_我使用ioctl正确吗?

我正在用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")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值