djangoに問題があります。djangoサーバーのブラウザーまたはビジネスロジックから別のdjangoサーバー、または同じサーバーでポートが異なるデータを送信して、リクエストを処理したいと思います。どのようにできるのか?ソケットを使って実現しようとしましたが、うまくいかないようです。
以下は私のコードです:
クライアントの要求を受け入れます。
def im(request):
userp=なし
試す:
userp = UserProfile.objects.get(user = request.user)
それ外:
パス
userpでない場合:
HttpResponse( "error")を返します
「111」を印刷
request.method == "GET"の場合:
jsonをインポートする
msg = json.loads(request.GET.get('msg'))
試す:
msg ['from_id'] = userp.id
if msg.get('type'、'')=='sync':#页面同しかし消息
msg ['to_id'] = userp.id
push_msg(msg)
HttpResponse( "success")を返します
それ外:
HttpResponse( "error")を返します
#return HttpResponseRedirect( "http://127.0.0.1:9000/on_message")
HttpResponse( "error")を返します
helper.py:push_msg:
def push_msg(msg):
「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)
試す:
印刷'メッセージの処理'
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()
それ外:
パス