输入URL是:http://fnserver <<
try:
response = requests.request("GET", url=url, verify=False, data=payload, headers=headers, auth=HTTPBasicAuth(username, passwd))
response.raise_for_status()
except requests.exceptions.HTTPError as errh:
print ("Http Error:",errh)
except requests.exceptions.ConnectionError as errc:
print ("Error Connecting:",errc)
except requests.exceptions.Timeout as errt:
print ("Timeout Error:",errt)
except requests.exceptions.RequestException as err:
print ("OOps: Something Else",err)
输出为:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connection.py", line 157, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/util/connection.py", line 61, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
这就是我想要的。
import traceback as tb
url = input("Please enter the URL: ")
print("Testing URL '{}'".format(url))
try:
r = requests.get(url)
except requests.exceptions.ConnectionError as e:
url = input("Connection error Enter new URL: \n")
tb.print_tb(tb,limit=0,file=None)
在这里是我找到它的地方https://docs.python.org/3.7/library/traceback.html