JSON-RPC server via Python - python

I need to implement a JSON-RPC server like this:
http://pasha.cdemo.applicationcraft.com/service/json
This server will be accessed from jQuery and I have to use Python for writing it.
What library should I use? Can you also give me an example of using that library?
Thanks.

I found cherrypy very easy to use (doesn't come with a predefined template engine or a database model, so it's IMO better than others when your server is producing json and is not a typical database).
Coupled with nginx and memcached can also be quite performant...

Python 2.6 comes with json module in the standard library which allows you to effective convert Python data structures to JSON responses.
For HTTP communications and request handling, you can use Python web frameworks like Pyramid, Django or HTTP server software like Tornado. It really much depends what do you need to process in your JSON-RPC calls.

Related

how can i send some data from php to python

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.

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.

Should I have a NodeJS server for react server-side rendering?

I have a API server which is written in python.
And I wanted to render my react components on server side.
So I searched the best practices and result was having a nodeJS server.
But in my thoughts, this is weird because I have to build a another server with different language. This is bad at maintainable, and maybe have an overload.
So I want to know having nodeJS server is usual. and also how do big companies do server-side rendering.
If it is as simple as wanting to use Python with React, you can use python-react to render React components.
As noted above, if you are using Flask, the templating is in Jinja2, but that isn't necessarily mutually exclusive to using React in your project.
This resource on using Flask/React may be of use to you, as well.

Generic REST/RPC API for Python (OpenCPU Equivalent)

A coworker and I are currently using OpenCPU to expose analytics we write in R to other applications via a REST API. There has been a need recently to leverage some python libraries in a similar manner. From the OpenCPU description:
OpenCPU is a system for embedded scientific computing and reproducible research. The OpenCPU server provides a reliable and interoperable HTTP API for data analysis based on R. You can either use the public servers or host your own.
Basically we update an R library on the server and it automatically exposes the updated and new functions at REST endpoints. It takes care of marshalling the data from JSON to S3 and then back to JSON. There is no need to manually configure routes with OpenCPU.
My question then, assuming we are operating in a secure environment, is does an equivalent for python exist? I've tried searching but have had little luck thus far.
Thanks!
Have you looked at IPython? I'm not sure about supporting library updates.

Gwt Front end with python web server

I am building a web app where I'd like to use GWT for generating the java script that runs in the browser but because I use other python packages (specifically scipy) I would like for the web server to be python (not a servlet engine). Has anybody done so ? Any pointers would be appreciated.
Thank you.
Ranga
GWT is a front-end framework. You can use any backend technologies, whether it is Python, PHP, Java, Node.js or any Server-side technologies. There is no limitations on this. I haven't use GWT for quite long now, You have the request builder to fetch data from the server.
Anyway, the simplest solutions that gonna scale for wide range of your devices is to build a REST API on the backend with python, its very simple, you have many simply python technologies like Bottle or Flask that does it in a giffy for you. Build your backend and make it follow REST conventions.
Then, on the level of GWT, it has to have something for fetching data from the webserver. I'll advise to make your data in JSON and then you'll be able to use technologies like GSON to convert your JSON serialised string into Java objects on the fly
Pyjamas looks like it was originally a port of GWT to Python:
https://wiki.python.org/moin/Pyjamas
This guy doesn't like it though, so you might consider the cons to doing this as well:
http://blog.pyjeon.com/?p=302

Categories