[英]Can django server send request to another server
I'm looking for help. 我正在寻求帮助。 My django server has instant messaging function achieved by django-socketio. 我的django服务器具有django-socketio实现的即时消息功能。 If I run the server by cmd 'runserver_socketio' then there is no problems. 如果我通过cmd'rowserver_socketio'运行服务器,那么没有问题。 But now I want to run server by 'runfcgi' but that will make my socketio no working. 但现在我想通过'runfcgi'来运行服务器,但这会使我的socketio无法正常工作。 So I want the socketio server handles the request which is conveyed by fcgi server. 所以我希望socketio服务器处理由fcgi服务器传达的请求。 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) Of course Django can make request to another server 1)当然Django可以向另一台服务器发出请求
I have not much idea about django-socketio
我对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 还有一个建议为什么你使用httplib你可以使用其他高级版本,如httplib2
或请求除了Django-Piston
专用于REST
请求你也可以尝试使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.