WebSocket + Django python WebService - python

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:
https://gist.github.com/1939836
It's adapted from a more complex source, I didn't tested this gist, it's long polling but tornado handlse WebSocket as well.
http://www.tornadoweb.org/documentation/websocket.html
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:
https://github.com/andrewgodwin/channels
It will probably be part of django 1.11 or 2.0

Although it's a bit complicated to setup (but probably the way to go), you could use gunicorn + gevent + socket.io .
I used this article to guide my way through it.
You might also look at server sent events (the article mentioned above looks at that too). If they suit your needs, it would be a bit easier to set up - since you don't have to set up socket.io and you don't need a client library. One catch though - SSE are not supported in IE.

Yeah, django is not all that great when it comes to asynchronous stuffs. My advice for you would be to use twisted as it has a lot of websocket libraries. If you really need to use django..you can make django act just as a pass through, for all the api stuff you build using twisted.

Related

integrating django with aiohttp/asyncio

I want to integate django with aiohttp/asyncio
for asynchronous programming and for websockets handling.
I know django has celery & django-channels to do asynchronous task and websocket server respectively but aiohttp is having both asynchronous and websocket server pre built in it and I found that framework more scalable and easy, compared to celery/django channels while creating a function to webscraping (I don't know if webscraping can be possible in celery I, havent tried it yet).
And it also supports async and await perfectly.
But my question is: How can we implement both django and aiohttp in a project? Instead using django's development server can we use aiohttp server to serve the site.
And are we able to integrate django with aiohttp function (like lets take an example: If I want to scrape a website of user submmited input to my database. Can I use await calls in my function while fetching the website and posting the following website to my django database? Or post the function results to another django function?)
And I want to know the disadvantages of integrating, if any?
And while posting your answer please could you post a sample practical example of integration instead suggesting me those libraries over github.
Maybe is time for considering Django >= 4.1 wich already have built-in Asynchronous Support
From the docs:
Django has support for writing asynchronous (“async”) views, along with an entirely async-enabled request stack if you are running under ASGI. Async views will still work under WSGI, but with performance penalties, and without the ability to have efficient long-running requests.
We’re still working on async support for the ORM and other parts of Django. You can expect to see this in future releases. For now, you can use the sync_to_async() adapter to interact with the sync parts of Django. There is also a whole range of async-native Python libraries that you can integrate with.

Secure web requests via Tornado with .htaccess

I have implemented a very small application with Tornado, where HTTP GET Requests are used to perform actions. Now I would like to secure these requests. What would be a preferable way? Using .htaccess? How can I realize that?
It doesn't have to be for certain requests, it should be for all requests running on a certain port.
If you based your application on the Tornado "Hello World" example then you probably haven't, but you really should consider writing your application as a WSGI application. Tornado has no problem with that, and the advantage is that your application now will run under a multitude of other environments (Apache + mod_wsgi to name but one).
But how does that solve your original problem? Well, just Google "WSGI authentication middleware", it'll yield plenty of hits. Basically, what that entails is transparently 'wrapping' your WSGI-application in another, one allowing you to completely decouple that aspect of your application. If you're lucky, and one of hits turns out to be a perfect fit, you might get away with not witing any extra code at all.
Since you mentioned .htaccess: it is possible to have Apache do the authentication in an Apache/mod_wsgi configuration.
.htaccess files are not supported by Tornado as far as I know. Look into setting up basic authentication on Tornado. Something like this: https://gist.github.com/660185 is probably what you want. You'll need to store your own user credentials however as Tornado has no baked in support for that as apache does with .htaccess files.

What Python Web Framework should I use with GWT to stream KML from Python Back-end?

I have a long-running process written in Python 2.7 that I would like to send KML files to my GWT application asynchronously as the KML files are generated.
I have been trying to determine what Python web framework I could use as the back-end with the Python process that could possibly allow the webapp to be hosted on Google AppEngine.
I was able to write a simple python webserver using Cherrypy that sent the kml using JSON from the back-end to GWT using an http request; however, I would like the files to be sent to GWT as they are generated since it may be several minutes between each one. What would be a relatively simple but effective way to achieve this? (Comet? Long-polling? Websockets?)
After researching more python web frameworks, I started experimenting with Tornado because it is non-blocking and seems like it could return data as it is generated possibly using long-polling as mentioned in this answer. However, it looks like GAE requires WSGI which would not allow a Tornado webserver to be non-blocking.
I have read answers to similar questions such as this one. However, I am not sure if updates in web frameworks, GWT, or GAE has changed what is the best option today, or whether some of these answers apply to my case.
What Python web framework would you recommend I use to send data to my asynchronous GWT app using long-polling or another method relatively simply? Could I use this web framework with GAE, or would I need to use something else?
If I understood the problem correctly you might don't need any special framework and you can solve it with what you have: Tasks API and Channel API.
With Tasks API you can perform long task and when the task is complete you can get a notification. You can combine it with the Channel API to push messages directly to the client when a particular task is complete.
You could use also the deferred library to simplify your life with tasks and maybe even using the PubNub for your push notifications, since the setup is easier and you can have many subscribers at the same time.

Creating a python web server to recieve XML HTTP Requests

I am currently working on a project to create simple file uploader site that will update the user of the progress of an upload.
I've been attempting this in pure python (with CGI) on the server side but to get the progress of the file I obviously need send requests to the server continually. I was looking to use AJAX to do this but I was wondering how hard it would be to, instead of changing to some other framerwork (web.py for instance), just write my own web server for receiving the XML HTTP Requests?
My main problem is that sending the request is done from HTML and Javascript so it all seems like magic trickery at the moment.
Can anyone advise me as to the best way to go about receiving these requests on the server?
EDIT: It seems that a framework would be the way to go. Would web.py be a good route to take?
I would recommend to use a microframework like Sinatra for Ruby. There seem to be some equivalents for Python. What python equivalent of Sinatra would you recommend?
Such a framework allows you to simply map a single method to a route.
Writing a very basic HTTP server won't be very hard (see http://docs.python.org/library/simplehttpserver.html for an example), but you will be missing many features that are provided by real servers and web frameworks.
For your project, I suggest you pick one of the many Python web frameworks and run your application behind Apache/mod_wsgi.
There's absolutely no need to write your own web server. Plenty of options exist, including lightweight ones like nginx.
You should use one of those, and either your own custom WSGI code to receive the request, or (better) one of the microframeworks like Flask or Bottle.

Python web server?

I want to develop a tool for my project using python. The requirements are:
Embed a web server to let the user get some static files, but the traffic is not very high.
User can configure the tool using http, I don't want a GUI page, I just need a RPC interface, like XML-RPC? or others?
Besides the web server, the tool need some background job to do, so these jobs need to be done with the web server.
So, Which python web server is best choice? I am looking at CherryPy, If you have other recommendation, please write it here.
what about the internal python webserver ?
just type "python web server" in google, and host the 1st result...
Well, I used web frameworks like TurboGears, my current projects are based on Pylons. The last ist fairly easy to learn and both come with CherryPy.
To do some background job you could implement that in pylons too.
Just go to your config/environment.py and see that example:
(I implemented a queue here)
from faxserver.lib.myQueue import start_queue
...
def load_environment(global_conf, app_conf):
...
start_queue()
It depends on your need if you simply use CherryPy or start to use something more complete like Pylons.
Use the WSGI Reference Implementation wsgiref already provided with Python
Use REST protocols with JSON (not XML-RPC). It's simpler and faster than XML.
Background jobs are started with subprocess.
Why dont you use open source build tools (continuous integration tools) like Cruise. Most of them come with a web server/xml interface and sometimes with fancy reports as well.
This sounds like a fun project. So, why don't write your own HTTP server? Its not so complicated after all, HTTP is a well-known and easy to implement protocol and you'll gain a lot of new knowledge!
Check documentation or manual pages (whatever you prefer) of socket(), bind(), listen(), accept() and so on.

Categories

Resources