Skip to content Skip to sidebar Skip to footer

Why Does Socket Interfere With Selenium?

I wrote a python script to check for an internet connection using socket (Checking network connection), then scrape html from yahoo finance using selenium. Very frequently (but not

Solution 1:

From the documentation:

socket.setdefaulttimeout(timeout)

Set the default timeout in seconds (float) for new socket objects. When the socket module is first imported, the default is None. See settimeout() for possible values and their respective meanings.

The problem is that setdefaulttimeout sets the timeout for all newly created sockets, therefore also for Selenium. It is a global socket library setting.

If you want to use a timeout for this socket instance only, use socket.settimeout(value) (doc).

definternet(host="8.8.8.8", port=443, timeout=1):
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(timeout) # correction from s.timeout(timeout)
    s.connect((host, port))
    s.shutdown(socket.SHUT_RDWR)
    s.close()
    returnTrueexcept OSError:  
    s.close()
    returnFalse

Post a Comment for "Why Does Socket Interfere With Selenium?"