A socket operation was attempted to an unreachable

2019-02-19 07:12发布

I am trying to make a REST client from django using httplib . But it is refusing the connection I tried the following

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)

but is giving following error

[Errno 10051] A socket operation was attempted to an unreachable network

What coluld be the issue?

Thank u

1条回答
虎瘦雄心在
2楼-- · 2019-02-19 07:48

The error indicates that your the machine you are running this script on cannot reach the destination IP address (10.0.2.2), as it doesn't have a network route configured from one to the other.

This is a problem with your internal network (10.x.x.x IP addresses are always private network addresses). If you are running this script on a different network from the machine you are trying to reach, you'll need a public IP address for it instead.

查看更多
登录 后发表回答