我尝试了以下代码,但获得连接异常。我可以使用php成功建立相同的连接。我的python代码抛出错误的任何原因:
def getUrl(self):
link = 'http://localhost:80/aztecgamesstudio'
data = requests.request("GET", link)
url = data.url
print(url)
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\Python37\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Python37\lib\site-packages\urllib3\util\retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=223): Max retries exceeded with url: /aztecgamesstudio/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001FA877A9EB8>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\dev\xampp\htdocs\aztecgamesstudio\app\libraries\Core.py", line 27, in <module>
init = Core()
File "c:\dev\xampp\htdocs\aztecgamesstudio\app\libraries\Core.py", line 16, in __init__
self.getUrl()
File "c:\dev\xampp\htdocs\aztecgamesstudio\app\libraries\Core.py", line 21, in getUrl
data = requests.request("GET", link)
File "C:\Python37\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python37\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python37\lib\site-packages\requests\sessions.py", line 668, in send
history = [resp for resp in gen] if allow_redirects else []
File "C:\Python37\lib\site-packages\requests\sessions.py", line 668, in <listcomp>
history = [resp for resp in gen] if allow_redirects else []
File "C:\Python37\lib\site-packages\requests\sessions.py", line 247, in resolve_redirects
**adapter_kwargs
File "C:\Python37\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "C:\Python37\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=223): Max retries exceeded with url: /aztecgamesstudio/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001FA877A9EB8>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
尝试此代码并获得301响应代码
import requests
result = requests.get('http://localhost/aztecgamesstudio', allow_redirects=False)
print(result)