gpt4 book ai didi

没有 socket.makefile() 的 python socket readline

转载 作者:太空狗 更新时间:2023-10-30 01:33:50 24 4
gpt4 key购买 nike

我正在尝试解析 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com