简体   繁体   English

django服务器可以向另一台服务器发送请求

[英]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.

相关问题 如何在django视图中向另一台服务器发送请求? - How to send a request to another server in a django view? 将数据从 Django 发送到另一台服务器 - Send data from Django to another server 我们能否将Django用作服务器,对单个请求定期发送多个响应? - Can we send multiple responses in intervals to a single request, Using Django as server? Django 将经过身份验证的用户发送到具有相同数据库的另一个 django 服务器 - Django send authenticated user to another django server with the same db jQuery函数无法将ajax请求发送到Django服务器 - jquery function cannot send ajax request to django server 如何从Mac应用程序向Django服务器发送HTTP请求? - How to send an HTTP request from a Mac app to a Django server? Django-如何正确地向服务器发送请求并要求运行Matlab程序? - Django - How to properly send a request to server and ask to run a matlab program? 如何将请求发送到另一台服务器并在金字塔视图中响应结果? - How to send request to another server and response the result in pyramid view? 我可以使用Radius客户端将注销请求发送到Radius服务器吗? - Can I send logout request to radius server with radius client? 创建“服务器中的客户端”以发送服务器请求 - Create “client in server” for send request for server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM