簡體   English   中英

django服務器可以向另一台服務器發送請求

[英]Can django server send request to another server

我正在尋求幫助。 我的django服務器具有django-socketio實現的即時消息功能。 如果我通過cmd'rowserver_socketio'運行服務器,那么沒有問題。 但現在我想通過'runfcgi'來運行服務器,但這會使我的socketio無法正常工作。 所以我希望socketio服務器處理由fcgi服務器傳達的請求。 可以嗎? 以下是我的代碼:

def push_msg(msg):
    params = urllib.urlencode({"msg":str(msg)})
    '''headers = {"Content-type":"text/html;charset=utf8"}
    conn = httplib.HTTPConnection("http://127.0.0.1:8000")
    print conn
    conn.request("POST", "/push_msg/", data=params, headers=headers)
    response = conn.getresponse()
    print response'''
    h = httplib2.http()
    print h
    resp, content = h.request("http://127.0.0.1:8000/push_msg/", method="POST", body=params)

url(r'^push_msg/$', 'chat.events.on_message')
chat.events.on_message:
def on_message(request):
    msg = request.POST.get('msg')
    msg = eval(msg)
    try:
        print 'handle messages'
        from_id = int(msg['from_id'])
        to_id = int(msg['to_id'])
        user_to = UserProfile.objects.get(id = msg['to_id'])
        django_socketio.broadcast_channel(msg, user_to.channel)
        if msg.get('type', '') == 'chat':
            ct = Chat.objects.send_msg(from_id=from_id,to_id=to_id,content=data['content'],type=1)
            ct.read = 1
            ct.save()
    except:
        pass  
    return HttpResponse("success")

我已經嘗試了很多次,但它無法工作,為什么?

1)當然Django可以向另一台服務器發出請求

我對django-socketio都不太了解

還有一個建議為什么你使用httplib你可以使用其他高級版本,如httplib2或請求除了Django-Piston專用於REST請求你也可以嘗試使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM