【问题标题】:Can't reach IP using Python httplib无法使用 Python httplib 访问 IP
【发布时间】:2018-11-11 20:36:37
【问题描述】:

我无法使用主机的 IP 地址连接到网络上的任何内容。我可以打开浏览器并连接,我可以 ping 主机就好了。这是我的代码:

from httplib import HTTPConnection

addr = 192.168.14.203
conn = HTTPConnection(addr)
conn.request('HEAD', '/') 

res = conn.getresponse()

if res.status == 200:
    print "ok"
else:
    print "problem : the query returned %s because %s" % (res.status, res.reason)

返回以下错误:

socket.error: [Errno 51] Network is unreachable

如果我将 addr var 更改为 google.com,我会收到 200 响应。我做错了什么?

【问题讨论】:

  • 你确定addr 不应该是字符串吗?
  • 192.168.14.203 是private IP address。您确定您的本地网络上存在这样的地址吗?
  • 它存在于我通过 VPN 连接的网络中

标签: python python-2.7 httplib


【解决方案1】:

您应该检查地址和您的代理设置。

为了发出 HTTP 请求,我推荐 requests 库。与httplib 相比,它更加高级和用户友好,并且可以轻松set proxies

import requests

addr = "http://192.168.14.203"
response = requests.get(addr)

# if you need to set a proxy:
response = requests.get(addr, proxies={"http": "...proxy address..."})

# to avoid using any proxy if your system sets one by default
response = requests.get(addr, proxies={"http": None})

【讨论】:

    猜你喜欢
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 2016-06-14
    • 1970-01-01
    • 2023-04-02
    • 2018-05-09
    相关资源
    最近更新 更多