没有 socket.makefile() 的 python socket readline

标签 python sockets

我正在尝试解析 HTTP 请求行(例如 GET/HTTP/1.1\r\n),使用 socket.makefile().readline()< 很容易(BaseHTTPRequestHandler 使用它),例如:

print sock.makefile().readline()

不幸的是,作为documentation说,当使用makefile() 套接字必须处于阻塞模式(它不能有超时);我怎样才能实现一个类似 readline() 的函数,它在不使用 makefile() 文件对象接口(interface)的情况下执行相同的操作并且读取的内容不超过需要(因为它会丢弃数据我需要之后)?

一个非常低效的例子:

request_line = ""
while not request_line.endswith('\n'):
    request_line += sock.recv(1)
print request_line 

最佳答案

四年半后,我会建议asyncio's Streams为此,下面是您可以使用 BytesIO

正确执行此操作的方法

请注意,每次检测到一行时,此实现都会“缩小”内存中的 BytesIO 对象。如果您不关心这一点,行数可能会少很多。

import socket
import time
from io import BytesIO

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 1234))
sock.setblocking(False)


def handle_line(line):
    # or, print("Line Received:", line.decode().rstrip())
    print(f"Line Received: {line.decode().rstrip()!r}")


with BytesIO() as buffer:
    while True:
        try:
            resp = sock.recv(100)       # Read in some number of bytes -- balance this
        except BlockingIOError:
            print("sleeping")           # Do whatever you want here, this just
            time.sleep(2)               #   illustrates that it's nonblocking
        else:
            buffer.write(resp)          # Write to the BytesIO object
            buffer.seek(0)              # Set the file pointer to the SoF
            start_index = 0             # Count the number of characters processed
            for line in buffer:
                start_index += len(line)
                handle_line(line)       # Do something with your line

            """ If we received any newline-terminated lines, this will be nonzero.
                In that case, we read the remaining bytes into memory, truncate
                the BytesIO object, reset the file pointer and re-write the
                remaining bytes back into it.  This will advance the file pointer
                appropriately.  If start_index is zero, the buffer doesn't contain
                any newline-terminated lines, so we set the file pointer to the
                end of the file to not overwrite bytes.
            """
            if start_index:
                buffer.seek(start_index)
                remaining = buffer.read()
                buffer.truncate(0)
                buffer.seek(0)
                buffer.write(remaining)
            else:
                buffer.seek(0, 2)

(原始答案非常糟糕,不值得保留(我保证),但应该在编辑历史记录中可用)。

关于没有 socket.makefile() 的 python socket readline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29023885/

相关文章:

python - Panda 的 DataFrame - 重命名多个同名列

python - 导入 swig 生成的模块时,Python 提示缺少删除方法

python - 有没有更好的方法在 Pyramid 中的 HTML 和 JSON 输出之间切换?

C套接字编程错误

sockets - 什么时候需要 TCP 选项 SO_LINGER (0)?

python-rope/ropevim 无法正常工作

python - GNU 并行数组参数

c - 如何正确使用 select() 中的 except_set?

sockets - 在 winCE 6 下使用 IPNAT API

Python - 1 程序,在不同端口但同一主机上发送和接收,这可能吗?