Can django server send request to another server
我在寻求帮助。我的django服务器具有django sockatino实现的即时消息功能。如果我通过命令"runserver"运行服务器,那么没有问题。但现在我想用runfcgi运行服务器,但这会使我的socket无法工作。所以我希望SOCKTONI服务器处理由fcgi服务器传递的请求。它能起作用吗?以下是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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可以向其他服务器发出请求
我对
还有一个建议,为什么你要使用httplib,你可以使用其他的高级版本,比如