try: senderSocket.send("Hello")except ?????: print "catch !"
By catching all socket.error exceptions, and re-raising it if the errno attribute is not equal to 111. Or, better yet, use the errno.ECONNREFUSED constant instead:

import errnofrom socket import error as socket_errortry: senderSocket.send("Hello")except socket_error as serr: if serr.errno != errno.ECONNREFUSED: # Not the error we are looking for, re-raise raise serr # connection refused # handle here

*

This error message...

You are watching: Socket.error: [errno 111] connection refused

MaxRetryError: HTTPConnectionPool(host="127.0.0.1", port=51379): Max retries exceeded with url: /session/2e64d2a1-3c7f-4221-96fe-9d0b1c102195/window (Caused by ProtocolError("Connection aborted.", error(111, "Connection refused")))...implies that the call to self.driver.close() method failed raising MaxRetryError.

A couple of things:

First and foremost as per the discussion max-retries-exceeded exceptions are confusing the traceback is somewhat misleading. Requests wraps the exception for the users convenience. The original exception is part of the message displayed.

Requests never retries (it sets the retries=0 for urllib3"s HTTPConnectionPool), so the error would have been much more canonical without the MaxRetryError and HTTPConnectionPool keywords. So an ideal Traceback would have been:

ConnectionError(: Connection refused)But again
sigmavirus24 in his comment mentioned ...wrapping these exceptions make for a great API but a poor debugging experience...

Moving forward the plan was to traverse as far downwards as possible to the lowest level exception and use that instead.

Finally this issue was fixed by rewording some exceptions which has nothing to do with the actual connection refused error.

Solution

Even before self.driver.close() within tearDown(self) is invoked, the try block within test_select_in_sina(self) includes finally where you have invoked driver.quit()which is used to call the /shutdown endpoint and subsequently the web driver & the client instances are destroyed completely closing all the pages/tabs/windows. Hence no more connection exists.

You can find a couple of relevant detailed discussion in:

PhantomJS web driver stays in memorySelenium : How to stop geckodriver process impacting PC memory, without callingdriver.quit()?

In such a situation when you invoke self.driver.close() the python client is unable to locate any active connection to initiate a clousure. Hence you see the error.

So a simple solution would be to remove the line driver.quit() i.e. remove the finally block.

See more: Driving Directions To 209 State Park Road Murfreesboro Ar 71958

tl; dr

As per the Release Notes of Selenium 3.14.1:

* Fix ability to set timeout for urllib3 (#6286)The Merge is: repair urllib3 can"t set timeout!

Conclusion

Once you upgrade to Selenium 3.14.1 you will be able to set the timeout and see canonical Tracebacks and would be able to take required action.

References

A couple of relevent references:

Adding max_retries as an argumentRemoved the bundled charade and urllib3.Third party libraries committed verbatim