0
votes

I'm looking for help. My django server has instant messaging function achieved by django-socketio. If I run the server by cmd 'runserver_socketio' then there is no problems. But now I want to run server by 'runfcgi' but that will make my socketio no working. So I want the socketio server handles the request which is conveyed by fcgi server. Can it work? Following is my code:

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")  

I have tried many times, but it can't work, why?

1
This might give you some ideas: stackoverflow.com/questions/5357635/…John Lyon

1 Answers

2
votes

1) Of course Django can make request to another server

I have not much idea about django-socketio

and one more suggestion why you are using httplib you can use other advance version like httplib2 or requests apart from that Django-Piston is dedicated for REST request you can also try with that