is "from flask import request" identical to "import requests"? - python

In other words, is the flask request class identical to the requests library?
I consulted:
http://flask.pocoo.org/docs/0.11/api/
http://docs.python-requests.org/en/master/
but cannot tell for sure. I see code examples where people seem to use them interchangeably.

No these are not only completely different libraries, but completely different purposes.
Flask is a web framework which clients make requests to. The Flask request object contains the data that the client (eg a browser) has sent to your app - ie the URL parameters, any POST data, etc.
The requests library is for your app to make HTTP request to other sites, usually APIs. It makes an outgoing request and returns the response from the external site.

Related

Redirect all requests from server to Flask app

Is there a way that I could redirect every http get request coming to server to flask application and if certain condition is satisfied the app would pass request on but if not the app would block access to requested url.
The request coming from client to server is not related to flask app but I want to filter all requests and give permission only to those who met certain condition.
I would lite to try this in flask, but I am open to every solution possible.
I personally think Flask is a great option to handle a proxy server.
I currently don't have a straight solution as I never did it before but this is what I found around that seems legit (and will implement myself in the feature).
MOST SIMPLE ONE
By #ziozzang in Github
Flask Dance package
Proxies and HTTPS
Stack Overflow
By #AArias
Other
Using NGINX-reverse proxy with Flask and Docker
By #stewartadam in github
Making a flask proxy server, online, in 10 lines of code.
How To Create A Flask Proxy
flask-behind-proxy 0.1.1

I got an API on AWS how to post json data using django?

It is working fine when I pass data from POSTMAN. I just want to call the API from Django the same as we do in the postman.
I’m not a django expert but you can look into a library like requests. In postman there’s an option to select code and you should be able to scroll to select python and see the options there. I’m not sure if that answers your question. If you’re looking to build a gui like postman using django you would have to setup the app and maybe a button underneath that would make a request using requests or some other library.
Well you could call http service using urllib, it is python built in library you don't have to install anything almost every features are there and there is also a third party http library alternative to urllib called requests with comprehensive features, which also uses urllib behind scene, it would be overkill just for calling simple http endpoint but it makes the life so easier BTW.
There will be no different calling from django, you can just wrap it under the view.
from urllib.request import urlopen
with urlopen('http://example.com') as res:
print(res.read()) # get request
# for post request with headers
from urllib.request import Request
req = Request(headers={'Content-Type': 'application/json'}, url='http://example.com', data='{"name": "Someone"}'.encode())
with urlopen(req) as res:
print(res.read()) # preform post request

AppEnginePlatformWarning - reason to use sockets?

In the Google App Engine standard environment, if you use urllib to make HTTPS requests, you'll get an AppEnginePlatformWarning which says you're using urlfetch instead of sockets.
I found the warning annoying, so I disabled it.
# Use the App Engine Requests adapter. This makes sure that Requests uses
# URLFetch.
requests_toolbelt.adapters.appengine.monkeypatch()
# squelch warning
requests.packages.urllib3.disable_warnings(
requests.packages.urllib3.contrib.appengine.AppEnginePlatformWarning
)
My question is - is there a good reason to switch to sockets? Specifically what is wrong with using urlfetch?
There's nothing wrong with using urlfetch, in fact it is the recommended method for issuing outbound HTTP(S) requests on GAE. From Issuing HTTP(S) Requests (emphasis on requests-related note mine):
App Engine uses the URL Fetch service to issue outbound HTTP(S)
requests.
For details about how the URL Fetch service is implemented and which
headers are sent in a URL Fetch request, see Outbound Requests.
Issuing an HTTP request
To issue an outbound HTTP request, use the urlfetch.fetch
method. For improved code portability, you can also use the Python
standard libraries urllib, urllib2, or httplib to issue HTTP
requests. When you use these libraries in App Engine, they perform
HTTP requests using App Engine's URL Fetch service. You can also use
the third-party requests library as long as you configure it to use
URLFetch.
The sockets support is rather the problematic one in GAE, it comes with a fairly long list of limitations and restrictions, see Sockets Python API Overview, in particular the Limitations and restrictions section.
The warning you see is not from GAE, it's from the 3rd-party requests library you use, which is why I highlighted the note in the above quote. IMHO it's safe to simply ignore/mask the warning in a GAE context.

Concurrent nested requests in a microservices environment built with Django

I'm developing a microservices architecture with Django in which services communicate each other through RESTful APIs.
A request coming from a client is served by a service, which then requires to make a new (nested) HTTP request to another service as it is shown in the picture below, in order to mantain decoupling between services.
In this scenario, it may happen that two different requests are fired by the client and served simultaneously by the first service. Then, one of the two nested requests to the second service remains unserved until a 503 error (Service Unavailable) occurs.
It seems that concurrency between nested requests is bad handled by the server.
RESTful APIs are implemented with Django REST framework and nested requests are made with the Python requests library. Here is an example:
from rest_framework.views import APIView
import requests
class FirstServiceView(APIView):
def get(self, request, format=None):
# Code...
# The nested requests to the second service
response = requests.get("http://127.0.0.1:8000/second_service/")
# Other code...
It's worth noting that this behaviour happens with both the development server of Django and Apache. Furthermore, no concurrency on the database is involved in this case.

Sending a string from C code to Flask

My server is hosted using Flask. I need to send a string from a C code to a page that is running on flask. I am using the socket programming in C to send data to server. But how do I receive it on the flask page? I tried using SocketIO and flask-sockets but was unable to do it properly. Any help would be appreciated. Thank you.
Flask talks HTTP. Write your C code to connect to Flask and send HTTP requests.
libcurl might be useful to you.
At the Flask end, I think that you need to work through the documentation to get an understanding of what Flask does and how to write your web app.
You can send a HTTP GET request with a parameter containing your string. In Flask you can access the GET parameters with the request object that is available in your view. The URL might be http://flaskserver/?string=hi+there. You can access the string by treating request as a dictionary, e.g. string_from_c = request.get('string').

Categories

Resources