尝试对python httplib中的无法访问的网络进行套接字操作
问题内容:
我正在尝试使用httplib从django创建REST客户端。但是它拒绝连接我尝试了以下
import hashlib
import hmac
from django.shortcuts import render_to_response
from django.template import RequestContext
def loginAction(request):
username=request.POST['email']
password=request.POST['password']
import httplib, urllib
params = urllib.urlencode({'username': username})
#hash username here to authenticate
digest=hmac.new("qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50", str(request.POST['password']),hashlib.sha1).hexdigest()
auth=username+":"+digest
headers = {"Content-type": "application/json","Accept": "text/plain","Authorization":auth}
conn = httplib.HTTPConnection("10.0.2.2",8000)
conn.request("POST", "/api/ecp/profile/", params, headers)
但给出以下错误
[Errno 10051] A socket operation was attempted to an unreachable network
可能是什么问题?
问题答案:
该错误表明您正在运行此脚本的计算机无法到达目标IP地址(10.0.2.2),因为它没有配置从一个到另一个的网络路由。
这是内部网络的问题(10.xxx IP地址始终是专用网络地址)。如果您正在与尝试访问的计算机不同的网络上运行此脚本,则需要一个公共IP地址。