Sending a string from C code to Flask - python

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').

Related

Use both Django REST framework and web sockets

I have an application built on Django. Initially, I constructed a few APIs using the Django REST API framework, which worked fine. Later, I implemented web sockets to listen to a server.
I started the application server on port 5010 and the socket on which this application listens to the server is 8010.
However, after implementing web sockets, my API calls started to fail(screenshot below).
Observation: I noticed that the API call doesn't fail after POST operation. The failure is at the point where I access the API at http://127.0.0.1:5010/subscribe/. That is, when I go to http://127.0.0.1:5010/subscribe/ in my browser, that is when I see the failure as shown in the above picture.
If I access the API http://127.0.0.1:5010/subscribe/ by deleting the code for web sockets, the API call works fine (screenshot below)
Please explain if using both REST API framework and web sockets would be possible in Django.
I guess you are using the wrong HTTP request in your case.
In Response header, the allowed HTTP request are POST and OPTIONS. But you are attempting to make GET request through web browser.
Try making a POST request instead.

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

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

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.

how do I create a client in django?

I am working on a web application that use Python => (server) and Django => (client):
I would like to know how to make (write) a client for Django; example: when i want to display a list I must send a request to the server and then display it on my web page. do you have any ideas to make that?? Thank you
If you're trying to get Django, while processing a view, to make an HTTP request to another server, you'll need to use either httplib on Python 2 or http.client on Python 3.
The Django Tutorial goes over templates.
Further, if you are trying to retrieve data without loading the page, AJAX is what you are looking for. Here are some AJAX references for Django.
Let us know if you have a specific question about a Django page.

Using Python to copy Flask requests to another Flask app

Let's say I have a Flask app running. When someone goes to any page, or makes any sort of request on the page, I want that request to be copied to another Flask app. Is there an already existing Flask plugin that would allow me to do so?
By copy I mean this:
My app is test.com. I have another Flask app running on a private machine on a private IP. When I get a GET request on test.com, I want the same GET request to be sent to the Flask app on the private app.
As others have said in the comments, the best kind of proxy is that provided by your web server. However sometimes you actually need your web application to do the proxying, in that case see this answer: Proxying to another web service with Flask

Categories

Resources