Skip to content
Advertisement

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

I’m writing a chat program. Each of my clients has an open get request to the server in a separate thread (and another thread for posting their own messages). I don’t want to have a lot of overhead. That is, clients don’t send get requests frequently to see if there have been any unseen messages. Instead, they always have exactly one open get request to get the new messages, and as soon as the server responded to them with new unseen messages, they immediately send another get request to the server to stay updated and so on. So on the client-side, I have something like this:

JavaScript

On the server-side, I have something like this:

JavaScript

and

JavaScript

And the logic behind this is that the servers want to do the long-polling, that is, it keeps GET/receive requests open in another busy-waiting thread. When any client sends a message to the server via POST/message it just adds this new message to other clients unseenMessages and so once their thread is running, they come out of the while True: loop, and the server gives them the new messages. In other words, I want to hold client’s GET/receive open and not respond it as long as I want. This process might take so long time. Maybe the chatroom is idle and there is no messages for a long time. Right now the problem I have is that as soon as my client sends its first GET/receive message, it gets this error, even though I have set the timeout value in GET/receive request to be so much.

JavaScript

=========================================================================== UPDATE:

The strange part is whenever I edit the GET/receive module to this:

JavaScript

everything works fine. But when I do :

JavaScript

It gives the same error to the client!

I mean the problem is because I pass the self object to another function to respond? maybe it interrupts the connection? I remember having the same problem in Java socket programming where I would encounter to some bugs sometimes when I wanted to use a socket to communicate in two functions? However, here I only want to communicate in the long-polling function not anywhere else.

=======================================

update: I also put my server and client code here. For brevity, I post the paste.ubuntu links here.

Client:

https://paste.ubuntu.com/p/qJmRjYy4Y9/

Server:

https://paste.ubuntu.com/p/rVyHPs4Rjz/

First time a client types, he enters his name and after that he starts sending GET/receive requests. Client can then send his messages to other people by sending POST/message requests. Any time a user send a message to the server, the server finds him (by his auth) and updates all other clients unseenMessages so that whenever their long-polling thread continued, they’ll get the new messages and their clients also send another GET/receive message immediately.

Advertisement

Answer

I have found the answer. I was trying to have a multithreaded server using single thread syntax! I followed this thread for having a multithreaded HTTP server Multithreaded web server in python

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement