Django 1.10 & Socket.IO with Python 3 - python

I'm trying to find some "django-socketio" repo to use in my project. I using django 1.10 and python3. I really searched but I do not found working examples with python3.
My poor workaround
I started node project and put socket.io inside route
In my django view I send returning data to node route with my django session
I manage session coming from django inside my node and emit inside
route to client.
This work but I can't believe this is a good solution.. Anyone have other ideas? Or working examples with python3 and socketio?
Thanks!

If you want to use Websockets and Django you should consider https://github.com/django/channels. The alternative in Python would be using python tornado http://www.tornadoweb.org/en/stable/ or aiohttp (Python3.4+) http://aiohttp.readthedocs.io/en/stable/. Many of the implementations of Django with asynchronousity through gevent are outdated, experimental or abandoned, I found this https://github.com/jrief/django-websocket-redis but it uses Redis so no reason to not going back to django-channels.
In my opinion, as Socket.io is a layer over Websockets you will not find any project that supports fully the Socket.io spec as a ws server in Python as it is a native Node.js not officially ported to Python project, at least the latest one you are probably using, if you really need Socket.io features stick to Node.js and create a simple REST API in Django to load the backend data asynchronously from Nodejs (the REST django API will always be synchronous by nature), this is the best shot you would likely have.

https://github.com/django/channels/blob/master/docs/getting-started.rst
django-channels works with python3 and django >=1.8 :)
And you can change pip to pip3

Related

Django Channels VS Django 3.0 / 3.1?

Can someone clarify the differences or complementarities between Django Channels Project and new Django native async support?
From what I understood, Django-Channels is a project that have been started outside of Django, and then, started to be integrated in the core Django. But the current state of this work remains confusing to me.
For example, today I'm using Django 2.2, and I'd like to add WebSocket support to my project. Should I:
Upgrade to the latest Django version?
Use Django Channels package?
Do both actions?
today I'm using Django 2.2, and I'd like to add WebSocket support to my project.
If you want to add websocket support to your app, at the moment you don't need to upgrade to django 3.0. Django 2.2 plus channels can do that - and for the time being is the best way forward. (Although there's absolutely no harm in upgrading to django 3.0 if you don't have any good reason not to). I will try and further explain why in this answer.
From what I understood, Django-Channels is a project that have been
started outside of Django, and then, started to be integrated in the
core Django. But the current state of this work remains confusing to
me.
Yes, my understanding is that channels started out as a project from one of the core Django developers (Andrew Godwin - who has also been instrumental in bringing about the async changes brought in Django 3.0). It is not included automatically if you just install Django, but it is officially part of the django project, and has been since september 2016 (see here). It's now on version 2.4 and so is an established and stable project that can be used to add websockets to your django app.
So What's going on with Django 3.x and async?
Whilst channels adds a way to add some async functionality to your django app, Django at it's core is still synchonous. The 'async' project that is being gradually introduced addresses this. The key thing to note here is that it's being introduced gradually. Django is made up of several layers:
WSGI server (not actually part of django): deals with the protocol of actually accepting an HTTP request
Base Handler: This takes the request passed to it from the server and makes sure it's sent through the middleware, and the url config, so that we end up with a django request object, and a view to pass it to.
The view layer (which does whatever you tell it to)
The ORM, and all the other lovely stuff you get with Django, that we can call from the view.
Now to fully benefit from async, we really need all of these layers to be async, otherwise there won't really be any performance benefit. This is a fairly big project, hence why it is being rolled out gradually:
With the release of django 3.0, all that was really added was the ability to talk to an ASGI sever, (rather than just a WSGI one).
When Django 3.1 is released (expected august 2020) it is anticipated that there will be capabilities for asynchronous middleware and views.
Then finally in django 3.2, or maybe even 4.0 we will get async capabilities up and down the whole of Django.
Once we get to that final point, it may be worth considering using the async features of Django for stuff like web-sockets, but at the moment we can't even take advantage of the fact we can now deal with ASGI as well as WSGI servers. You can use Django with an ASGI server, but there would be no point as the base handler is still synchronous.
TLDR
Django channels adds a way to deal with protocols other than HTTP, and adds integrations into things such as django's session framework and authentication framework, so it's easy to add things like websockets to your django project. It is complete and you can start working with it today!!.
Native async support is a fundemental re-write of the core of Django. This is a work in progress. It's very exciting, but won't be ready to really benefit from for a little while yet.
There was a good talk given at last years djangoCon outlining the plans for async django. You can view it here.

How do I create a web application using Core Python without a framework?

I want to build an web application using python as a backend so, as I did not learned any of the web frameworks that are available for python I want to know is there any way to create backend for an app without frameworks.
While there is a discussion in the comments about the utility of frameworks, I am trying to answer the question at its face value.
WSGI
Python frameworks like Flask and Django both at the end are WSGI applications. WSGI (Web Service Gateway Interface) is a PEP specification which defines how the server and client must communicate. If I were to start from scratch, I would probably start with learning about WSGI and even try implementing a little ping-pong server with it. The read the docs page here https://wsgi.readthedocs.io/en/latest/learn.html lists a number of pages to learn about it.
Werkzeug
Once the WSGI specs are understood then one can attempt building a simple library that will wrap the core concepts into reusable functions and modules for easily writing an application. Here Werkzeug can be a good guide to make one understand the different aspects. https://www.palletsprojects.com/p/werkzeug/
Your own application
Based on the understanding of the WSGI spec and the Werkzueg library you can go on to write your applications either from scratch, or write a library like werkzueg yourself and then use it to write an application.
Finally reimplement the same app in Flask or Django to see what frameworks offer.
If it's something small for internal use you can use
https://docs.python.org/3/library/http.server.html

Python authentication/authorization framework

I'm developing a REST API in python with tornado and I'm going to implement the authentication and authorization, trying to avoid lock-in to other big projects i.e django. I'm looking around through forums and SO too and I've fond a solution that could fit: repoze.who/what.
It seems a good solution but I'm a bit scared about the activity of the projects (repoze.what last release seems to be 1.0.9 (2010-03-04)) and if it can work with async IO.
Anyone have tried it and/or knows something else?
Few months ago, I wrote python-auth. It's simple to use with a RESTFul API.
Install:
pip install auth
Run:
auth-server
Usage:
In your code:
from auth.client import Client
service = Client('srv201', '<Auth-Server IP:PORT>')
print(service)
service.get_roles()
service.add_role(role='admin')
Or use RESTFul API

How to run nginx + python (without django)

I want to have simple program in python that can process different requests (POST, GET, MULTIPART-FORMDATA). I don't want to use a complete framework.
I basically need to be able to get GET and POST params - probably (but not necessarily) in a way similar to PHP. To get some other SERVER variables like REQUEST_URI, QUERY, etc.
I have installed nginx successfully, but I've failed to find a good example on how to do the rest. So a simple tutorial or any directions and ideas on how to setup nginx to run certain python process for certain virtual host would be most welcome!
Although you can make Python run a webserver by itself with wsgiref, I would recommend using one of the many Python webservers and/or web frameworks around. For pure and simple Python webhosting we have several options available:
gunicorn
tornado
twisted
uwsgi
cherrypy
If you're looking for more features you can look at some web frameworks:
werkzeug
flask
masonite
cherrypy (yes, cherrypy is both a webserver and a framework)
django (for completeness, I know that was not the purpose of the question)
You should look into using Flask -- it's an extremely lightweight interface to a WSGI server (werkzeug) which also includes a templating library, should you ever want to use one. But you can totally ignore it if you'd like.
You can use thttpd. It is a lightweight wsgi server for running cgi scripts. It works well with nginx. How to setup thttpd with Nginx is detailed here: http://nginxlibrary.com/running-cgi-scripts-using-thttpd/
All the same you must use wsgi server, as nginx does not support fully this protocol.

What python based ajax push servers are good to work with

I have a web application im currently working on in python. I'm using Django as my web framework currently. I intend to integrate real-time web features into the application using ajax push. However, I'm a bit confused as to what python based push server i should adopt. I've heard of a few of them, Orbited, Gevent, Tornado, but my main issue right now is that i'm a green horn when it comes to realtime web applications. I'd like to use python and i'd like a framework with good documentation.
A lot of people choose gevent for webapps with real-time features because of the speed, ease of use and a number of supporting packages.
Take a look at these packages:
gevent-socketio
django-socketio
and these tutorials:
Evented Django part one: Socket.IO and gevent
New and hot, part 4: Pyramid, Socket.IO and Gevent
Real-time Web Apps with Django and WebSockets
Hope this helps.
APE seems a cool thing.
I think it's not a problem to make it work with PDjango as a backend.
I think the most popular is tornado open-sourced by facebook. It also has pretty decent documentation.

Categories

Resources