How to clean up socket connection to printer

loial jldunn2000 at gmail.com
Tue Jul 9 05:39:21 EDT 2013


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.

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?

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

    try:
        sock.close()
        del sock
    except Exception, e:
        pass

Is there anything else I should do in these circumstances to ensure that my socket connection is closed OK? 




More information about the Python-list mailing list