我有一个 django 问题。我想将来自浏览器或我的 django 服务器上的业务逻辑的数据发送到另一个 django 服务器或只是相同的服务器但不同的端口,以处理请求。我能怎么做?我曾尝试使用套接字来实现,但似乎没有用。
以下是我的代码:
接受客户的请求:
def im(请求):
userp = 无
尝试:
userp = UserProfile.objects.get(user = request.user)
除了:
经过
如果不是用户:
返回 HttpResponse("错误")
打印'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(味精)
返回 HttpResponse("成功")
除了:
返回 HttpResponse("错误")
#return HttpResponseRedirect("http://127.0.0.1:9000/on_message")
返回 HttpResponse("错误")
helper.py:push_msg:
def push_msg(味精):
打印'111'
参数 = 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(请求):
msg = request.POST.get('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)
如果 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()
除了:
经过