首页 > 解决方案 > http.client 在 Python 中不适用于本地主机

问题描述

我的代码:

import http.client

h1 = http.client.HTTPConnection('127.0.0.1')
h1.request('GET', '/')

我也尝试使用 localhost 而不是 IP,但它也不起作用。我确信我的 apache 正在运行,因为我在浏览器中进行了测试。

Traceback (most recent call last):
  File "/home/kali/i1.py", line 4, in <module>
    h1.request('GET', '/')
  File "/usr/lib/python3.9/http/client.py", line 1279, in request
    self._send_request(method, url, body, headers, encode_chunked)                                                                                                             
  File "/usr/lib/python3.9/http/client.py", line 1325, in _send_request                                                                                                        
    self.endheaders(body, encode_chunked=encode_chunked)                                                                                                                       
  File "/usr/lib/python3.9/http/client.py", line 1274, in endheaders                                                                                                           
    self._send_output(message_body, encode_chunked=encode_chunked)                                                                                                             
  File "/usr/lib/python3.9/http/client.py", line 1034, in _send_output                                                                                                         
    self.send(msg)                                                                                                                                                             
  File "/usr/lib/python3.9/http/client.py", line 974, in send                                                                                                                  
    self.connect()                                                                                                                                                             
  File "/usr/lib/python3.9/http/client.py", line 945, in connect
    self.sock = self._create_connection(
  File "/usr/lib/python3.9/socket.py", line 844, in create_connection
    raise err
  File "/usr/lib/python3.9/socket.py", line 832, in create_connection
    sock.connect(sa)

标签: pythonhttp.client

解决方案


推荐阅读