代码之家  ›  专栏  ›  技术社区  ›  MoreScratch

无法使用Python httplib访问IP

  •  0
  • MoreScratch  · 技术社区  · 7 年前

    我无法使用主机的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变量更改为 google.com

    1 回复  |  直到 7 年前
        1
  •  0
  •   pawroman    7 年前

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

    对于进行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})