gpt4 book ai didi

Python http requests.exceptions.ConnectionError : ('Connection aborted.' , RemoteDisconnected ('Remote end closed connection without response' ))

转载 作者:行者123 更新时间:2023-12-04 15:14:05 28 4
gpt4 key购买 nike

我正在编写一个聊天程序。我的每个客户端在一个单独的线程(以及另一个用于发布自己的消息的线程)中都有一个对服务器的开放获取请求。我不想有很多开销。也就是说,客户端不会频繁发送 get 请求来查看是否有任何看不见的消息。相反,它们总是只有一个打开的 get 请求来获取新消息,一旦服务器用新的未见消息响应它们,它们就会立即向服务器发送另一个 get 请求以保持更新等等。
所以在客户端,我有这样的事情:

def coms():
headers = {'data': myAut.strip()}
resp = requests.get("http://localhost:8081/receive", headers=headers,timeout=1000000)
print(resp.text)
t = threading.Thread(target=coms, args=())
t.start()
在服务器端,我有这样的事情:
def do_GET(self):

if self.path == '/receive':
auth=self.headers['data']

#Using auth, find who has sent this message
u=None
for i in range(len(users)):
print(users[i].aut,auth)
if users[i].aut==auth:
u=users[i]
break


t=threading.Thread(target=long_Poll,args=(u,self))
t.start()
def long_Poll(client,con):

while True:

if len(client.unreadMessages) != 0:
print("IM GONNA RESPOND")
con.end_headers()
con.wfile.write(bytes(client.unreadMessages, "utf8"))
client.unreadMessages=[]
break
con.send_response(200)
con.end_headers()
而这背后的逻辑是服务器要做长轮询,即保持 GET/receive请求在另一个忙等待线程中打开。当任何客户端通过 POST/message 向服务器发送消息时它只是将此新消息添加到其他客户端 unseenMessages所以一旦他们的线程运行,他们就会从 while True: 中出来循环,服务器向他们提供新消息。换句话说,我想持有客户的 GET/receive打开并且只要我愿意就不会回复它。
这个过程可能需要很长时间。可能是聊天室闲着,很久没有消息了。
现在我遇到的问题是,只要我的客户发送第一个 GET/receive消息,即使我在 GET/receive 中设置了超时值,它也会收到此错误要求这么多。
C:\Users\erfan\Desktop\web\client\venv\Scripts\python.exe C:\Users\erfan\Desktop\web\client\Client.py
Hossein
Welcome to chatroom Hossein ! Have a nice time !
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
chunked=chunked,
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\urllib3\connectionpool.py", line 426, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\urllib3\connectionpool.py", line 421, in _make_request
httplib_response = conn.getresponse()
File "C:\Users\erfan\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1321, in getresponse
response.begin()
File "C:\Users\erfan\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 296, in begin
version, status, reason = self._read_status()
File "C:\Users\erfan\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 265, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\urllib3\connectionpool.py", line 727, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\urllib3\util\retry.py", line 410, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\urllib3\packages\six.py", line 734, in reraise
raise value.with_traceback(tb)
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
chunked=chunked,
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\urllib3\connectionpool.py", line 426, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\urllib3\connectionpool.py", line 421, in _make_request
httplib_response = conn.getresponse()
File "C:\Users\erfan\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1321, in getresponse
response.begin()
File "C:\Users\erfan\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 296, in begin
version, status, reason = self._read_status()
File "C:\Users\erfan\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 265, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\erfan\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\erfan\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\erfan\Desktop\web\client\Client.py", line 13, in coms
resp = requests.get("http://localhost:8081/receive", headers=headers,timeout=1000000)
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\erfan\Desktop\web\client\venv\lib\site-packages\requests\adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
================================================== ==========================
更新:
奇怪的部分是每当我编辑 GET/receive模块到这个:
 def do_GET(self):
while True:
pass
一切正常。
但是当我这样做时:
def do_GET(self):
t=threading.Thread(target=long_Poll,args=(self))
t.start()

def long_Poll(con):

client =None
while True:
pass
它给客户端同样的错误!
我的意思是问题是因为我将 self 对象传递给另一个函数来响应?也许它会中断连接?我记得在 Java 套接字编程中遇到过同样的问题,当我想使用套接字在两个函数中进行通信时,有时会遇到一些错误?但是,在这里我只想在长轮询功能中进行交流,而不是其他任何地方。
========================================
更新:
我还将我的服务器和客户端代码放在这里。为简洁起见,我在这里发布了 paste.ubuntu 链接。
客户:
https://paste.ubuntu.com/p/qJmRjYy4Y9/
服务器:
https://paste.ubuntu.com/p/rVyHPs4Rjz/
客户第一次打字时,他输入他的名字,然后他开始发送 GET/receive要求。客户端然后可以通过发送 POST/message 将他的消息发送给其他人。要求。任何时候用户向服务器发送消息,服务器都会找到他(通过他的身份验证)并更新所有其他客户端 unseenMessages这样每当他们的长轮询线程继续时,他们就会收到新消息,他们的客户也会发送另一个 GET/receive立即留言。

最佳答案

我找到了答案。我试图拥有一个使用单线程语法的多线程服务器!
我跟随这个线程拥有一个多线程的 HTTP 服务器
Multithreaded web server in python

关于Python http requests.exceptions.ConnectionError : ('Connection aborted.' , RemoteDisconnected ('Remote end closed connection without response' )),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64658898/

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