Python 3.x 服务器关闭时客户端请求被卡住
我有一个客户端,它向服务器发出简单的请求。它通常工作正常,但如果服务器关闭或根本不存在,请求就会被卡住,而不做任何其他事情 这是我的代码:Python 3.x 服务器关闭时客户端请求被卡住,python-3.x,python-requests,Python 3.x,Python Requests,我有一个客户端,它向服务器发出简单的请求。它通常工作正常,但如果服务器关闭或根本不存在,请求就会被卡住,而不做任何其他事情 这是我的代码: import requests from requests.exceptions import Timeout class Client: def __init__(self): # do things for initilization def do_request(self): requ
import requests
from requests.exceptions import Timeout
class Client:
def __init__(self):
# do things for initilization
def do_request(self):
request_url = 'http://fhksdjhfksdhfk.com'
try:
response = requests.post(request_url, timeout=5)
print('Response received from registration:', response) <-- Never reach this statement
if response.status_code != 200:
print('Request error at', request_url, 'error:', response.reason)
else:
print('Request finished successfully')
except Timeout:
print('Request timeout')
导入请求
从请求。异常导入超时
类客户端:
定义初始化(自):
#为初始化做事情
def do_请求(自我):
请求http://fhksdjhfksdhfk.com'
尝试:
response=requests.post(请求url,超时=5)
print('Response received from registration:',Response)except子句的缩进是否正确?是的,缩进是否正确,我复制它时问题中的缩进是错误的。如果我的服务器启动并运行代码,我的意思是,它会继续运行。