Python Eve with custom webserver - python

I would like to exploit Python Eve features, but I have a custom web environment where I have my Request objects and (can be disabled) a Router.
I know Python Eve is built on top of Flask and those features are already there, but I would like somehow to wrap/adapt my custom requests into Python Eve / Flask ones.
I have a process acting as a webserver (It receives and sends messages in protocols different from HTTP). I was searching for a standard way to interface it to Eve or Flask. I found out WSGI.
To further clarify: Imagine you have your ESB which is able to vehicle HTTP requests.
If you want to handle those requests with Eve, you should build a gateway/bridge.
It means, implementing something that:
Receives the proprietary or not standard protocol containing the request
Extracts the most important parameters from the request, such as URL, QUERY_STRING, HTTP method and so on...
Fills a WSGI Environment with those parameters following the PEP
Runs the WSGI application (in our case an Eve instance)
We get the response from the WSGI application
Pack the response back into your proprietary or custom protocol
Send back to requestor
A really simple example can be found at http://ivory.idyll.org/articles/wsgi-intro/what-is-wsgi.html

I am not sure what you mean. Do you want to use Eve with a different (custom) framework other than Flask? That's going to be really hard without an almost total rewrite, as Eve is, in fact, a Flask application (a subclass actually).

Related

How to interpose RabbitMQ between REST client and (Python) REST server?

If I develop a REST service hosted in Apache and a Python plugin which services GET, PUT, DELETE, PATCH; and this service is consumed by an Angular client (or other REST interacting browser technology). Then how do I make it scale-able with RabbitMQ (AMQP)?
Potential Solution #1
Multiple Apache's still faces off against the browser's HTTP calls.
Each Apache instance uses an AMQP plugin and then posts message to a queue
Python microservices monitor a queue and pull a message, service it and return response
Response passed back to Apache plugin, in turn Apache generates the HTTP response
Does this mean the Python microservice no longer has any HTTP server code at all. This will change that component a lot. Perhaps best to decide upfront if you want to use this pattern as it seems it would be a task to rip out any HTTP server code.
Other potential solutions? I am genuinely puzzled as to how we're supposed to take a classic REST server component and upgrade it to be scale-able with RabbitMQ/AMQP with minimal disruption.
I would recommend switching wsgi to asgi(nginx can help here), Im not sure why you think rabbitmq is the solution to your problem, as nothing you described seems like that would be solved by using this method.
asgi is not supported by apache as far as I know, but it allows the server to go do work, and while its working it can continue to service new requests that come in. (gross over simplification)
If for whatever reason you really want to use job workers (rabbitmq, etc) then I would suggest returning to the user a "token" (really just the job_id) and then they can call with that token, and it will report back either the current job status or the result

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.

CherryPy: How to process a request before it has reached the application method?

I want to be able to catch the arguments of the methods of my CherryPy application before the method itself. But I'm not sure if there is a way to do it in CherryPy or with standard python. It should look something like this:
HTTP Request --> Parser to catch the arguments --> CherryPy who passes the request to method
My goal is to capture the input and output to a server without disturbing the code in the method itself.
Here is how I check post methods for a valid csrf token our server generates.
def check_token(self=None):
# whenever a user posts a form we verify that the csrf token is valid.
if cherrypy.request.method == 'POST':
token = cherrypy.session.get('_csrf_token')
if token is None or cherrypy.request.params.get('csrf_token') == None or token != cherrypy.request.params['csrf_token']:
raise cherrypy.HTTPError(403)
cherrypy.tools.Functions = cherrypy.Tool('before_handler', check_token)
Hope this helps!
The standard Python way of handling HTTP requests is WSGI. WSGI allows stacking the processing components called WSGI middleware. That is where you can modify requests before they get to the framework's internals. CherryPy is WSGI-compliant, so the middleware can be used with it.
However, CherryPy is more than just a framework, it is also a web server. If you're using it as a server, it's most likely a cherrypy.quickstart() call. To add a middleware, it needs to have some more coding to build a site "tree" producing a WSGI app and to connect the app to the CherryPyWSGIServer class. This article seems to be explaining it well. As usual however, I recommend using uWSGI for running Python WSGI applications instead of CherryPy's built-in server. It has tons of features and overcomes GIL issue.
Additionally you could use page handlers / tools to manipulate requests before they are actually processed. See docs.

python microframeworks and the requests library

Working on a side project in python that both initiates http requests and has a small web server. It got me thinking - it seems like every python microframework has its own model for accessing properties of the http request (e.g. the underlying request object that you can use to get at the querystring parameters, headers, etc. and the underlying response object for setting the status code, response headers, etc). They all allow you access to the same data and they've all kind of re-invented the wheel.
Are there any microframeworks that use the Request and Response objects from the popular requests library instead of having their own implementation? It seems like the requests library is becoming the canonical way to do http requests in python, so this would make a framework especially minimal. It would also be cool when making apps that essentially glue other services together because forwarding/wrapping requests would be trivial. You could just change the .url attribute on the incoming request and call .prepare() to forward the request (yes, for simple forwarding doing it at the webserver level makes more sense, but you get the idea).
Or if there aren't any frameworks that do this in particular, are there any with similar benefits i.e. the incoming http request object also works as an http client?
Edit: Wanted to point out that this is how the http Request object works in Go, that's partly what inspired my question. From the net/http library "A Request represents an HTTP request received by a server or to be sent by a client."
Flask is based on Werkzeug. Werkzeug itself is using Request which itself is using BaseRequest. But this is not the Requests library.
Note that there was a plan to create an httpcore library using both Requests and Werkzeug, but that seems to have stopped. That said both projects are kicking.
Some people have modified Flask in their apps using Requests

HTTP framework for a python based server serving mobile applications

I am working on building a python powered back-end server for a mobile application. I am using MongoDB hosted on the Cloud for storage. The communication between the Application and the server will be mostly JSON. There wont be any HTML involved in this transfer as such.
I am looking for a framework to build my server. I did some research and found (django, Pylons etc) are for full stack server and is not needed for my requirements.
I am confused on how to go about selecting an HTTP framework (web.py, cherrypy etc).
also, i might use Mongoengine as the ODM. I am not sure if this decision is anyway going to affect the framework selection
any ideas?
I'd recommend bottle or flask, both of which are very minimal and impose no constraints on you whatsoever. In the case of bottle, it's particularly easy to build a simple RESTful server since it will automatically return JSON from a route that returns a dict:
from bottle import route, run
#route('/user_info/:id'):
def user_info(id):
user = get_user(id) # ...do something, lookup user, whatever
return {'first_name': user.first_name, 'last_name': user.last_name}
run()
Pyramid Framework doesnt enforce a templating engine (not a full stack) or a persistence/storage system. It has json and jsonp renderer (no html at all).
It also has a scaffold for jquery mobile app (I havent tried this, so I dont know).
I personally prefer web.py, but in your case it is possible to start with basic wsgi app, or something simple as http://werkzeug.pocoo.org/ because it doesn't enforce a specific template engine or database adapter.

Categories