【问题标题】:Am I using ioctl correctly?我正确使用 ioctl 吗?
【发布时间】:2014-02-28 23:35:57
【问题描述】:

我正在 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 macos sockets ioctl kqueue


【解决方案1】:

请注意,在fcntl.ioctl 的文档中,他们提到了arg 的可接受类型(ioctl 操作的参数)。在某些情况下,(像这样),您需要传递一个buffer,或者一个支持其接口的对象。特别是,当您想要接收返回值时。你只是传递一个整数。

【讨论】:

    猜你喜欢
    • 2015-06-25
    • 2022-01-08
    • 2011-08-07
    • 2011-09-26
    • 2019-05-03
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多