How to clean up socket connection to printer

Ulrich Eckhardt ulrich.eckhardt at
Tue Jul 9 16:05:08 CEST 2013

Am 09.07.2013 11:39, schrieb loial:
> I have a socket application that is connecting to a HP printer via port 9100.
> Occassionally I get a "Connection reset by peer" error which I am
> trapping and exiting the script with an error message.

Strange. Why does the remote terminate the connection?

> That works Ok, the issue I have is that the next time I run the
> script  I get "Connection refused" from the printer, which
 > suggests that the printer still thinks the port is is busy,
 > though nothing is printing. I suspect that in some way my socket
 > connection has not been closed correctly?

I'm assuming you are using TCP. Getting a "connection refused" rather 
means that there is no server process that is listening on that port. It 
sounds a bit as if the printer was kind-of rebooting itself, which first 
resets the existing connection and then, after a rebooting, opens the 
port again for connections.

Question here:
1. Does the printer accept connections again after some time?
2. Does the printer accept connections if you close and re-open the 
Python interpreter?
3. Is there actually a limit to the number of concurrent connections? In 
other words, what happens when you try to create a second connection 
without closing the first?

> When I get the "Connection rest by peer" error, I attempt to close
> the  port as follows :

This is useless, the connection is already closed at that point.

Your description suggests that it is a remote problem. I still wouldn't 
rule out that it is somehow caused by your code though, but without 
seeing that, it's impossible to tell.

Good luck!


More information about the Python-list mailing list