Crawler overtime error socket.timeout: timed out/NameError: name ‘socket‘ is not defined

Question 1: socket. Timeout: timed out

Source code:

import urllib.request#Get a get request

import urllib.parse #Get a pos request

import urllib.error
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}
data1 = urllib.request.Request("http://httpbin.org/post", headers=headers)

response = urllib.request.Request("http://httpbin.org/get", headers=headers)
response1 = urllib.request.urlopen(response, timeout=0.01)
# response1 = urllib.request.urlopen(response)
print(response1.read().decode("utf-8"))

Abnormal operation result:

Traceback (most recent call last):
  File "D:\PycharmProjects\pythonProject\douban\test1\testurllib.py", line 53, in <module>
    response1 = urllib.request.urlopen(response, timeout=0.01)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 517, in open
    response = self._open(req, data)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 1375, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 1350, in do_open
    r = h.getresponse()
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1345, in getresponse
    response.begin()
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\socket.py", line 704, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

Process finished with exit code 1

Question 2: NameError: name ‘socket’ is not defined

Source code:


import urllib.request#Get a get request

import urllib.parse #Get a pos request
import urllib.error
try:
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}
    # data1 = urllib.request.Request("http://httpbin.org/post", headers=headers)

    response = urllib.request.Request("http://httpbin.org/get", headers=headers)
    response1 = urllib.request.urlopen(response, timeout=0.01)
    print(response1.read().decode("utf-8"))

except socket.timeout as e:
# except Exceptio as e:
# except urllib.error.URLError as e:##error socket.timeout: timed out
    print("time out!")

Abnormal operation result:

Traceback (most recent call last):
  File "D:\PycharmProjects\pythonProject\douban\test1\temp.py", line 19, in <module>
    response1 = urllib.request.urlopen(response, timeout=0.01)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 517, in open
    response = self._open(req, data)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 1375, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 1350, in do_open
    r = h.getresponse()
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1345, in getresponse
    response.begin()
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\lord\AppData\Local\Programs\Python\Python39\lib\socket.py", line 704, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\PycharmProjects\pythonProject\douban\test1\temp.py", line 22, in <module>
    except socket.timeout as e:
NameError: name 'socket' is not defined

Solution:

Refer to the network resources, delete and install Python again, and still report an error.

By introducing socket library, problem one and problem two are solved.

import socket    ##Introducing the socket library

Correct code:

import socket ## Introduce the socket library

import urllib.request # Get a get request

import urllib.parse # Get a pos request
import urllib.error

try:
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}
    # data1 = urllib.request.Request("http://httpbin.org/post", headers=headers)

    response = urllib.request.Request("http://httpbin.org/get", headers=headers)
    response1 = urllib.request.urlopen(response, timeout=0.01)
    print(response1.read().decode("utf-8"))

except socket.timeout as e:
# except Exceptio as e:##normal
# except urllib.error.URLError as e:## error socket.timeout: timed out
    print("time out!")

Results of correct operation:

time out!

Process finished with exit code 0

Conclusion:

If try/except statement is not used, the program will report error a
a try/except statement has been added. It should be noted that the exception name of except should correspond to a. the error “NameError: name ‘socket’ is not defined” is reported here because the socket library is not imported.


Read More: