Socket Programming for chat on Google App Server - python

Disclaimer: I am a novice programmer
I am currently following a tutorial: http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server
To build a chat application on the iPhone using socket servers. For other purposes, I am using Google App Engine to maintain the backend of my app and hold onto other pieces of data. It only makes sense to have all my backend code located in one area so I was wondering whether Google App Engine will support my socket Programming as there seems to be quite a few restrictions as such: https://developers.google.com/appengine/docs/python/sockets/#limitations-and-restrictions
In fact it almost looks as if there are too many restrictions, however google on the page said that there are "Libraries that import socket, such as poplib or nntplib, and that don't violate the limitations and restrictions listed below, should work without modification." meaning that there are things that I can do to modify my work to allow it to work on the Google App Engine.
My Question: Is it possible to use my learning of socket programming to maintain a backend for my chat on the google app engine? If there is, how do I modify my file if I need to. If there isn't, what app server should I look into so that I can at least hold my chat backend on another server if not at google app engine. If you think that I should take another method altogether to implement chat in my iPhone app, I would love to hear that as well. Thank you for your input.

I think you shall not open the socket yourself, you should use APNS on iPhone and Google Cloud Messaging on Android, so it's not your app that will open (send keepalives, reopen when closed, reopen when connectivity change, etc...) the TCP socket. Also you'll be able to receive data (be spawned) when even if your app is closed.
Received messages (from APNS/GCM) can contains the actual data, or simply be a "Hey, you may go check for messages on the server". To send message you may simply use an HTTP request.

Related

Connect REST API on Pi to the Cloud

I am relatively new to programming. I have a project where I need to control things like pumps and read data from sensors. I currently have a flask webserver set up on a raspberry and can access that website only from within my local network.
My objective is to change my flask webserver to a flask REST APi. Then I want to set up my raspberry pi on a cloud/iot platform and control my pi over the cloud via the flask REST api.
Any idea on how to best do this? Ive been researching it a lot and could use some help.
I would setup the api endpoints like you discussed. Those endpoints I assume would allow you to control the sensors and whatever other hardware is attached to the GPIO. Then as you mentioned you would expose that API to some sort of an IOT platform. AWS offers a nice solution however there are many.
I have come a relatively long way from what I knew when asking this question. For other new programmers trying to figure out how to bring their project/idea to reality here is what I did to deploy my local flask web server on the pi to "the cloud":
I used a company named linode to host my web server on, but there are many other cloud platforms you could choose from like digital ocean and heroku. All I did was purchase their entry level plan, connect via ssh to the new server, copy over the files containing my web server, and add security provisions. Then you can use ssh to connect to your server and use the terminal to do what you need. This was the processes that I followed to deploy to the cloud. There are an abundance of options and ways to do this, but I found this option relatively flexible and cost effective...giving me the chance to participate in the IoT world.
I know this information is obvious to a lot of experienced programmers, but for the ones just getting started I hope this explanation can provide some clarity on your way forward.

Python using the correct socket

I am using Google Cloud and Google App Engine, and I want to have one of our service API's to use socket connection instead of a regular HTTP connection where a request is received and a response is sent once per query.
I need this socket connection so that there will be a constant connection and stream of current data being sent back to the user whenever it is updated.
I have read several documents and the Google App Engine docs for sockets suggests using the socket library. But at the bottom of the docs, they provide a sample app that uses Google App Engine with sockets, but the socket in the example app is made from nntplib and not socket. How many python libraries are there where a socket can be made / used of and which socket should I be using for this kind of connection.
It says in python docs nntplib is used for implementing a news reader or poster, so I am unsure if this is the correct socket connection type I should be using for sending back current data of users online to users on the front end.
https://docs.python.org/2/library/nntplib.html
Should I just use
socket.socket()
EDIT:
https://cloud.google.com/appengine/docs/standard/python/sockets/#making_httplib_use_sockets
If I understood well the type of connection you need, Websockets would be a good idea so you can create a full-duplex communication channel. However Google App Engine (GAE) doesn’t have support for Websockets yet. As per this post it will be implemented soon for GAE Flexible.
There's a Feature Request for Websockets in GAE, you can track the updates in this link
In the meanwhile if you need Websockets you have to use a Compute Engine instances
Regarding the support for Sockets in GAE, you can use any library that import socket (poplib, nntplib etc) that don’t violate some limitations and restrictions listed here.
My original intentions were to not use any additional services or fees such as the many great ones suggest by #dhauptman, but it seems like according to the Google App Engine docs for sockets (Python 2.7):
Python Sockets Google App Engine
it seems like since sockets only support outbound sockets, I can either just create a socket connection with subscribed events and whenever an event occurs and updates, just query the API for a task handler and send it back as a response.
Another option would be to use httplib library instead of Google App Engines urlfetch. When using the httplib library, it can be configured to use a socket connection in the app.yaml file. This will overcome the problem of reaching urlfetch limits.

Client-Server framework for python

I'm currently working on a University project that needs to be implemented with a Client - Server model.
I had experiences in the past where I was managing the communication at socket level and that really sucked.
I was wondering if someone could suggest an easy to use python framework that I can use for that purpose.
I don't know what kind of details you may need to answer so I'm just going to describe the project briefly.
Communication should happen over HTTP, possibly HTTPS.
The server does not need to send data back or invoke methods on the clients, it just collects data
Many clients send data concurrently to server, who needs to distinguish the sender, process the data accordingly and put the result in a database.
You can use something like Flask or Django. Both frameworks are fairly easy to implement, Flask is much easier than Django IMO, although Django has a built in authentication layer that you can use, albeit more difficult to implement in a client/server scenario like you need.
I would personally use Flask and JWT (JSON Web Tokens), which will allow you to give a token to each client for authentication with the server, which will also let you differentiate between clients, and you can use HTTPS for your SSL/TLS requirement. It is tons easier to implement this, and although I like django better for what it brings to the table, it is probably overkill to have you learn it for a single assignment.
For Flask with SSL, here is a quick rundown of that.
For JWT with Flask, here is that.
You can use any database system you would like.
If I understood you correctly you can use any web framework in python. For instance, you can use Flask (I use it and I like it). Django is also a popular choice among the python web frameworks. However, you shouldn't be limited to only these two. There are plenty of them out there. Just google for them.
The implementation of the client depends on what kind of communication there will be between the clients and the server - I don't have enough details here. I only know it's unidirectional.
The client can be a browser accessing you web application written in Flask where users send only POST requests to the server. However, even here the communication will bidirectional (the clients need to open the page which means the server sends requests back to the client) and it violates your initial requirement.
Then it can be a specific client written in python sending some particular requests to your server over http/https. For instance, your client can use a requests package to send HTTP requests.

Build a TCP proxy on Google App Engine

I'm trying to figure out how to build a TCP proxy on GAE (Google App Engine). I would ordinarily do it using twisted networking engine but GAE doesn't allow frameworks. I'm also pretty new to internet and networking technologies in general.
Basically I have a proxy server and I'd like to use GAE as a TCP proxy to relay everything to the primary proxy server. All the GAE front ends are connected to the back end by google fiber, so if I make the back end near the primary proxy server, it should make it super fast regardless of where I'm connecting from.
Unfortunately GAE doesn't allow me to control ports at all and everything that I'm reading either tells me how to configure a TCP proxy on a server that I'm in complete control of or how to configure a proxy where I type the url into a webpage in the browser. Something along the lines of making a personal http://www.hidemyass.com/proxy/ type of website.
I'd like to set it up so I can simply tell chrome to ignore certificate errors (it connects to a dynamic IP using HTTPS so there's no way to sign it but I trust myself) and put the proxy info into chrome.
Edit: I'd prefer to write it in python but I can do any language
Thanks in advance
P.S. Please don't give answers like just use GoAgent or tor or something. They don't fulfill my purpose.
If you're simply trying to proxy HTTP requests like GoAgent does then have a look at the URLFetch documentation for Google App Engine.
URL Fetch Python API Overview
If you're trying to proxy anything else, then Daniel is correct.
This isn't the sort of thing you can use GAE for.
I don't know where you got the idea that GAE "doesn't allow frameworks". Of course it does, anything that speaks WSGI (eg Django, Flask, Pylons) is fine. But GAE is a web platform: it's not an appropriate place to try and write any sort of bare-metal networking platform. Apart from anything else, bandwidth on GAE is fairly expensive.
And also I don't know where you think the GAE "front ends" are, as opposed to the "back ends". GAE is not split that way, AFAIK.
I don't really understand what exactly you are trying to do, but it sounds like a content delivery network (CDN) like Akamai might be more appropriate.

Communicating between appengine and an application

I'm working on a web interface which currently runs using PHP and communicates locally to a python script.
I'm moving the web side to appengine, which so far is going well when being used locally, I'm currently communicating from the appengine app to the python app via get requests that are handled by the python script.
The problem is, that obviously the machine running the python script will be behind a firewall, I've never needed to do this before and am not sure on how to implement this best.
The only idea I have so far is for the python script to send post requests to the appengine with some data and then as a response, send back some other data. The only problem with this is that the web interface should update the client quite fast.
Any ideas?
Take a look at ProtoRPC Python API: https://developers.google.com/appengine/docs/python/tools/protorpc/overview
Though it is still marked as experimental, it seems to be a decent framework for what you are trying to do - send messages back and forth between the apps.
Since you said your local app runs behind a firewall, I'm assuming you cannot open up an endpoint and protect it with some form of authentication.
Once you have messages flowing, you can either use Channel API to keep the front-end updated: https://developers.google.com/appengine/docs/python/channel/overview
Or if you want to go more basic, just implement long/short polling through AJAX.
Sorry with the limited amount of info you have provided, that's all I can think of right now. Please feel free to post more details and I'll try to help further.

Categories