4

I have a django problem. I want to send datas which are from browser or business logic on my django server to another django server or just the same server but different port, to handle the request. How can I do? I have tried to achieve using socket,but it seems no working.

Following is my code:
accept the client's request:
def im(request):
    userp = None
    try:
        userp = UserProfile.objects.get(user = request.user)
    except:
        pass
    if not userp:
        return HttpResponse("error")
    print '111'
    if request.method == "GET":
        import json
        msg = json.loads(request.GET.get('msg'))
        try: 
            msg['from_id'] = userp.id
            if msg.get('type', '') == 'sync': #页面同步消息
                msg['to_id'] = userp.id
            push_msg(msg)
            return HttpResponse("success")
        except:
            return HttpResponse("error")
        #return HttpResponseRedirect("http://127.0.0.1:9000/on_message")
    return HttpResponse("error")

helper.py:push_msg:
def push_msg(msg):
    print '111'
    params = str(msg)
    headers = {"Content-type":"application/x-www-form-urlencoded", "Accept":"text/plain"}
    conn = httplib.HTTPConnection("http://127.0.0.1:9000/push_msg/")
    conn.request("POST", "/cgi-bin/query", params, headers)

url(r'^push_msg/$', 'chat.events.on_message')
events.py: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 

2 Answers 2

2

use python requests module to do this requests has more features then httplib2 and it is very easy to use http://docs.python-requests.org/

0
0

I have used httplib2 to accomplish something similar. From the httplib2 documentation try:

import httplib2
import urllib
data = {'name': 'fred', 'address': '123 shady lane'}
body = urllib.urlencode(data)
h = httplib2.Http()
resp, content = h.request("http://example.com", method="POST", body=body)

You should then be able to handle the POST in your second django server, and return the appropriate results to the first django server.

6
  • Thanks!!!I will try your ways. Actually, the server that handles the request above is run by django socketio(cmd:runserver_socketio), and the original server is run by 'runserver' or 'runfcgi' command line which is use to handle common request. That is, I want socketio server handles instant messaging, but the request is conveyed by another server which accepts all the request from browser. And, the browser is connected to the socketio server, and I want the server directly send response to browser. Did you process similar problems before? Thanks, look forward to your reply.
    – liao
    Aug 25, 2012 at 5:40
  • I've never used django socketio before. I've only used runserver with different ports or apache for local testing.
    – smang
    Aug 25, 2012 at 5:44
  • Another question, does the url "url(r'^push_msg/$', 'chat.events.on_message')" have problems?
    – liao
    Aug 25, 2012 at 5:47
  • It doesn't appear to. Are you having issues with it?
    – smang
    Aug 25, 2012 at 5:51
  • I means all request is handled by views.py, can I use event.py?
    – liao
    Aug 25, 2012 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.