Dealing with HTTP and websocket connections on the backend - python

I am working on a game I want to support over iOS/Android/Browser and thinking Websockets is what I want to use for the communication. I use python and so found that I should be using Tornado.
I am trying to understand websockets a little better and their integration in browsers.
Will the messages over the websocket connection also contain the HTTP cookies for the connection? If not can I send it?
How is the HTTP connection for the web page linked to the websocket connection? I mean how will I know they are coming from the same webapp on the server side?
The Tornado wiki page says in the performance section that Tornado can be set up with nginx as the front end. How does that work? I thought Tornado and nginx have to be running on separate machines since both listen on port 80 and also because nginx does not understand WS protocol. What am I missing?
Also it will be great if someone can point me to any resources I can read up on about either Tornado or websocket that could help me.

The websocket is setup by sending an ordinary http request to the server, this request will contain all the stored cookies for the domain. If you do a native implementation for e.g. Android you can use libraries like Autobahn|Android, the API allows you to set cookies for the websocket handshake.
You can set a cookie when first loading the page to maintain a session identifier.
In that scenario they would be running 4 Tornado instances (on different ports, but not port 80) and Nginx on port 80 as a load-balancer, spreading the incoming client requests to the Tornado instances, see running Tornado and Nginx on same server for a configuration example. Recent versions of Nginx does support websockets, see e.g nginx + python + websockets.

Related

REST API in Python over SSL

I am creating a REST API. Basic idea is to send data to a server and the server gives me some other corresponding data in return. I want to implement this with SSL. I need to have an encrypted connection between client and server. Which is the best REST framework in python to achieve this?
You can choose any framework to develop your API, if you want SSL on your API endpoints you need to setup SSL with the Web server that is hosting your application
You can obtain a free SSL cert using Let's encrypt. You will however need a domain in order to be able to get a valid SSL certificate.
SSL connection between client and server does not depend on the framework you choose. Web Servers like Apache HTTPD and Nginx act as the public facing reverse proxy to your python web application. Configuring SSL with your webserver will give you encrypted communication between client and server
On assumption that you are talking about communication between REST Apis and some other stack like flask(A different server).
Rest apis can be used to communicate data with any type of platform as long as they agree on a common protocol to share data.
Data can be shared using xml, yaml or json. Your rest apis can be on any stack you like.
Architecture will be something like:-
Your main site(microservice or monolithic) <=> REST Apis(microservices)
You can use djangorestframework or any other you prefer.

do i need to rewrite my twisted chat server if i want to support web sockets?

I have an existing chat socket server built on top of twisted's NetstringReceiver class. There exist Android/iOS clients that work with it fine. Apparently web sockets use a different protocol and so it is unable to connect to my server.
Does a different chat server need to be written to support web sockets?
“Yes.”
If you'd like a more thorough answer, you'll have to include more information in your question.

Difference between Socket Programming and Django Form submission

i am learning socket programming and Django web developing. As what i know socket is used for the communication between client and server. Dose this mean that when using and submitting Form in Django, there will be a socket created and submitted to the server?
Dose this mean that when using and submitting Form in Django, there will be a socket created and submitted to the server?
The short answer: yes.
Your terminology is a bit off: a socket isn't "submitted" to the server. A socket is used for sending data to, and receiving data from, somewhere else.
The longer answer is of course much more complicated. The longest answer fills textbooks.
There are a few layers to peel off here.
HTML forms are transmitted to the server using HTTP.
HTTP is an application-layer protocol which is built on top of TCP.
TCP is a transport layer protocol (built, of course, on something else).
So, in the simplest case*, when the form is submitted, the browser sends an HTTP POST to the server. The browser's HTTP implementation will open a TCP connection to the server, and that's a socket. Through that socket, the HTTP message is transmitted, and the server's response is received.
So a socket is a much, much lower-level construct than an HTML form.
From Wikipedia, here's a full view of the layers of the networking stack:
If you find this stuff interesting, I recommend taking a course on computer networking.
* No Connection: Keep-alive, for instance.
The socket is created but not submitted to the server. Django is for HTTP server (port 80 and some other custom used ones). It is a library built around sockets listening/connecting to said port and with extra added features needed in Web development.
Socket programming is more general since it is lower level than Django and it allows using any port number you desire.
On the ISO OSI stack Django stays on level 7 while socket programming is at level 4.
Sockets are just one method of communicating between client and server, but not the method Django is using when processing a form submission. It is most likely sending a HTTP POST request with parameters that the server listens for.

Communicating with a Node.js server application

I am building a chat application that consists of a Django web backend with a Node.js/socket.io powered chat server. There will be instances when changes made via the web interface (e.g. banning a user) need to be pushed immediately to the chat server. I can think of the following options:
Use a Python-based socket.io client to interface directly with the server (what are some good Python clients?)
Use redis or a message queue to do pub/sub of events (seems like overkill)
implement a simple TCP wire protocol on a secondary localhost-only port (this could be done using the built-in Node and Python TCP libraries)
What would be the best option?
Expose a Restful API on the chat server. Then your Django web application can easily make API calls to modify state in the chat server.
Doing anything else is more complicated and most likely unnecessary.

How to maintain server-initiated FTP connection between client requests in Python/Django?

I'm going to use ftplib to open up an FTP connection to an FTP server provided by the user. The client will be sending FTP commands to my django server via Ajax, which will then be forwarded to the FTP server the user provided. However, I'd like to not have to create a new FTP server connection every time the client sends an FTP command. In other words, I want to keep the FTP connection alive between requests by the client.
How would I do this? Would some sort of comet implementation be best? I was initially planning to use WebSockets until I discovered my host won't allow it. =\
You'll need to use a persistent connection framework as what you're trying to achieve really isn't what HTTP was meant for (in the sense that HTTP commands are stateless and independent), and thus not what Django is built for. There are a number of options, but since it seems you are in a restricted environment you'll need to do some research to determine what's best.
Switch hosts. Webfaction allows websockets with dedicated IP at around $20 per month.

Categories

Resources