python django websocket_WebSocket + Django python WebService

bd96500e110b49cbb3cd949968f18be7.png

I was wondering how to create a django webservice (responds with XML) with websockets.

I have already a django webservice which accepts xml requests, parse those requests, makes a database query, creates a response xml and send that xml back to the requester/browser. Just a normal HTTP XML request, where the response is shown as xml within the browser.

But how would i now create a websocket django webservice? Lets say i would like to send a xml response to the requester/browser with the latest data from database whenever a new magical event occurs.

I have read a lot of posts and blogs but it was kinda all too general. Can i solve this only with django + apache or do i need something else next to django and another server only to handle websockets?

I am right now using django 1.3, Apache + wsgi but i would be ready to switch any configuration that would work.

Update:

There are many possible websockets out there,

http://pypi.python.org/pypi?:action=search&term=websocket&submit=search

but which one could be used in my case?

解决方案

Sorry but django handles async requests very very poorly as it is wsgi. You'll be limited by your number of parallel instance if you have to handle real users. The best solution is to use tornado or node.js.

Tornado handles websocket and long polling brilliantly. Here is my wrapper to allow getting user and sessions from a parallel tornado thread:

It's adapted from a more complex source, I didn't tested this gist, it's long polling but tornado handlse WebSocket as well.

update:

Avoid django-websocket for production use. Even the main developer recommends against it.

I recommend Tornado because it's an awesome technology which is damningly faster/lighter than django. It may be useful for some simple cases. You'll need to configure apache/nginx anyway so, at least get "faster web pages" feature available.

Django-Desktop-Notification focuses on chrome browser and require node.js.

update (01/2016):

Mozilla gave money to django in late 2015 to solve this particular issue, the current most promizing implementation made by a django core dev is this one:

It will probably be part of django 1.11 or 2.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值