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.
Related
I am designing a micro-service based system that will be accessed by a python SDK, from an application.
The SDK will be used to access machine learning models hosted as micro-services in a backend system.
I understands that GRPC relies on protobuf, which is lightweight and support streams. We do need to send data vectors to the hosted models, so it is appealing to use protobuf.
The question is more about the GRPC, as I understand it is using HTTP/2.
GRPC seems useful for accessing an API hosted on the backend, however there is also a need to keep an open live connection to receive general update events from the server.
For example I would like to have a “context” where incoming events can arrive, mainly for asynchronous communication, for example after several model invocations are performed, the system might use aggregated data on the backend and send an event when a prediction crosses some threshold.
Hypothetical usage example:
ctx = BackendSystemSDK.connect(APIGatewayHost,port)
ctx.registerCallback(SomeAlertCallbackFunc)
...
ctx.applyModel(‘model1’,SomeVector1)
ctx.applyModel(‘model2’,SomeVector2)
...
When program terminates
ctx.close()
I played with GRPC and tested a client and server successfully in python.
However I am not sure about implementing the “context” open connection idea with GRPC.
It is more like a pub/sub concept, however I could not find any examples for local testing such architecture. I did see examples for Google cloud, however it is not relevant as I want to host everything on-premise using kubernetes for scale.
The system should support heavy load of requests, for example processing multiple incoming video streams, per frame, so 24 requests per minute for 20-50 cameras (or more) could easily be the case.
Is GRPC good fit for such scenario?, not only for inter-microservice communication but also for the main API access gateway?
And for the "context", Maybe it should be implemented as live connection part with websockets or other protocol? I really want to simplify the development and use a single technology.
I could not fully understand from the documentation whether HTTP/2 and GRPC supports a long running bi-directional open connection.
I have a running python application that needs to receive some data and process them. and I also have a PHP server that can get these data. I want to send JSON data from PHP to my python app.
anyway except running a python web server and send data to it, or insert into DB and get from DB with python?
thanks.
I tried using python cherryPy web server.
#Niklas D It would be easier to answer your question, if you can give some more context about the application or use case you want to solve.
Some further possibilities are:
Glue Code (I never did it with python and php only C++ with python, but you should be able to find examples on the internet e.g. https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages#PHP )
Messaging Systems like RabbitMQ, ActiveMQ, ZeroMQ, etc.
Redis (I know you said except writing to a database, but Redis provides some features for publish subscribe https://redis.io/commands/pubsub which allows you to write to Redis from the one side and get data on the other side without polling the db all the time, which is the issue you have with using a database I guess) It's a bit easier to setup and use, than a messaging system.
TCP connection between the python and php application. https://medium.com/swlh/lets-write-a-chat-app-in-python-f6783a9ac170
If you want to send data to a python application using web protocols, i.e send POST, GET requests etc then you need to create a python web app to receive and handle those requests. Which in turn needs to be running off a webserver or you could build serverless functions to handle this, see https://serverless.com/
If you want to get data using a python application, i.e the python app sends POST and GET requests etc to your php app to ask for the JSON payload you can build an app using python's standard requests library https://docs.python.org/3/library/urllib.request.html or better still us the Requests package http://docs.python-requests.org/en/master/
Or you could do something and save the JSON file to disk and then open it with your python app. You'd need to set up scheduling or make your php app execute python code on the server... This last suggestion is a bad idea please don't unless your app is isolated and not publicly accessible or you know how to lock down your security.
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.
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.
I have a Python/Flask backend and an Angular frontend for my website. At the backend there is a process that occasionally checks SQS for messages and I want it to push a notification to the client which can then in turn update an Angular controller. What is the best way to do this my existing technologies?
To be able to push to the client, you'll have to implement web socket support in some fashion. If you want to keep it in python/flask, there is this tutorial on how to do that with gevent:
http://www.socketubs.org/2012/10/28/Websocket_with_flask_and_gevent.html
In that article, Geoffrey also mentions a SocketIO compatible library for python/gevent that may allow you to leverage the SocketIO client-side JS library, called "gevent-socketio".
That may reduce how much work you have to do in terms of cross-browser compatibility since SocketIO has done a lot of that already.
Here is a pretty good tutorial on how to use SocketIO in AngularJS so that you can notify the AngularJS model when an event comes in from SocketIO:
http://www.html5rocks.com/en/tutorials/frameworks/angular-websockets/
If you don't want to host the web socket backend, you could look to a hosted service like PubNub or Pusher and then integrate them into AngularJS as a service. You can communicate with these services through your Python app (when the SQS notification happens) and they'll notify all connected clients for you.
I know this is a bit late, but I have done pretty much exactly what you ask for (though without Angular).
I ended up having a separate process running a websocket server called Autobahn, which listens to a redis pub/sub socket, the code is here on github
This allows you to send push notifications to your clients from pretty much anything that can access redis.
So when I want to publish a message to all my connected clients I just use redis like this:
r = redis.Redis()
r.publish('broadcasts', "SOME MESSAGE")
This has worked fairly good so far. What I can't currently do is send a push notification to a specific client. But if you have a authentication system or something to identify a specific user you could tie that to the open websockets and then be able to send messages directly to a specific client :-)
You could of course use any websocket server or client (like socket.io or sock.js), but this has worked great for me :-)