gpt4 book ai didi

python - 无法在单个连接中发送多个请求数据 — 套接字错误

转载 作者:行者123 更新时间:2023-12-01 05:38:46 26 4
gpt4 key购买 nike

这个问题来 self 之前的问题。我需要一次性将一些数据多次发送到服务器(在此特定示例中为 2 次):

conn = httplib.HTTPSConnection("www.site.com")
conn.connect()
conn.putrequest("POST", path)
conn.putheader("Content-Type", "some type")
fake_total_size = total_size / 2 # split a file into 2 parts

conn.putheader("Content-Length", str(fake_total_size))
conn.endheaders()

chunk_size = fake_total_size
source_file = open(file_name)

#1 part
chunk = source_file.read(chunk_size)
conn.send(chunk) # ok!
response = conn.getresponse()
print response.read() # ok!

#2 part
chunk = source_file.read(chunk_size)
conn.send(chunk) # OPS! [Errno 32] Broken pipe
response = conn.getresponse()
print response.read()

source.close()

也就是说,我想在单个连接中发送多个请求,而不需要关闭或重新创建它。

请注意,该错误肯定不是因为服务器故障,而是因为套接字,但为什么呢?

如何消除错误?

更新:

同样的错误:

#1 part
chunk = source_file.read(chunk_size)
conn.send(chunk) # ok!
#response = conn.getresponse()
#print response.read()

更新2:

仍然没有运气:

conn.putheader("Connection", "Keep-Alive")
#.........
chunck_count = 4
fake_total_size = total_size / chunck_count

for i in range(0, chunck_count):
print "request: ", i
chunk = my_file.read(chunk_size)
# conn.putrequest("POST", path) -- also causes the error
conn.send(chunk)

response = conn.getresponse()
print response.read()

回复:

request:  0
request: 1
request: 2 # --> might not even exist sometimes
Unexpected error: [Errno 32] Broken pipe

最佳答案

连接已关闭,因为您调用了 conn.getresponse() 并且服务器关闭了它。除了传递 Connection: keep-alive header 并希望服务器遵守之外,您在连接端对此无能为力。

如果您想发送另一个 HTTP 请求,则必须以 conn.putrequest("POST", path) 或类似的内容开头。

关于python - 无法在单个连接中发送多个请求数据 — 套接字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18201152/

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