invoke python script and populate results on webpage - python

I created a simple python script which takes a URL as input and once passed will do curl using multiple proxies and show the response code, now I want to create a webpage where others can use(my colleagues) as it will help them too, I want to create simple webpage which let them select set of proxy addresses, and input URL and upon submission, it will run the script on a machine(webserver) and populate the result to webpage using dynatable or datatable frameworks, but am not sure how or if it is possible as I didn't worked much in webserver thing, I want to know what tools I will need and how do I design it.
If python script can be called in terminal(as it needs to run curl) and show result on webpage based on output from script(which I will export to csv file), how can I do that? what to use xampp, wamp, lamp etc ?

You need a framework for this, something that will listen to your request coming from the front-end (webpage), there are tons out there as python framework, you can check bottle framework as a starting point.
So the flow would be something below.
1. from webpage, a request is sent to the backend
2. backend receive the request and run your logic (connecting to some server, computing logic, etc)
3. once backend process is done, backend then send the response to webpage
you can either use a REST approach or use templating functionality of the framework

You will need a request. You can do this in JavaScript with an ajax request there are frameworks to hook it up straight to your python which allow you not to code the JavaScript https://www.w3schools.com/xml/ajax_intro.asp there are many JavaScript frameworks that will make this easier/ shorter to code.

Related

Backend script passing data to client-side script

Say that I have a Python Flask server running which has a backend script that produces a number and a string.
How can I pass the number and string from the backend to a script that runs of client-side so that the user can run it rather than the server?
Example:
backend script data_producer.py produces "asdaslkdjasdlksja" and 18 from its functions
I want to pass "asdaslkdjasdlksja" and 18 to a Brython or JavaScript HTML embed ( tag) so that it can be processed in the browser and the results be sent back to my server.
Edit: I realized that I can just use Jinja2's "{{ }}" when rendering a template so that I can use data in an HTML script embed
The question you asked is too broad. It's almost equivalent to asking, how you can connect two computers. Since you haven't even specified any data type, the first thing that comes to my mind is using sockets which is as low level as you can get.
A more high level and appropriate approach would be to use an HTTP REST server (with flask-RESTful), since you already have a flask server running.
However, there's another million ways to transfer data between two Python scripts, from WebSockets, WebRTCs, sshing, the new IPFS to even emails. Now most of them would probably be overkill, so I would suggest you to make a simple REST server and make the client send GET or POST requests to it.
After looking at the new edit, I still think a REST api is the best option. Since you can easily make a GET or POST request using the fetch api in Javascript. In Brython you can use ajax to do the same thing.

How can I get data from python scripts to my React web app?

I currently have a simple React Web App. I want to be able to "call" a python program, which will take in input as arguments, run, and give me back the output in a JSON format for example.
What are the steps for making this type of pipeline? I've heard of terms like using ajax calls and setting up Pyton web servers, but I'm not sure of where to start.

How do I run a Python script on a processing server?

I have written my Python script to take an inputted argument via command line.
My script simply take a URL (inputted via command line), then runs the script; which counts how many lines of HTML code is on the URL page. Its a very simple script.
I would like to put this script on my website. If you click a button on my webpage, the URL of my webpage is sent to the script, then it process the information, and returns it to my website.
How would I be able to do this? What would the back-end architecture look like? How can I increase my processing speeds? Will my script be able to process multiple clicks from different users simultaneously?
There are a couple ways you could do this. The first and the simplest would be CGI or Common Gateway Interface. The second would be a python web framework like flask or django , which you could configure via wgsi so like you said, it would run when it's url is accessed.

bottle framework: getting requests and routing to work

I have written a webapp using traditional cgi. I'm now trying to rewrite it with bottle
The page is simple...the user fills out a form, hits submit and the data object is sent to a python script that used to live in my cgi-bin
The python script generates an image, and prints the url for that image out to standard out
On callback, I use javascript to display the newly generated image on the page formatted with html.
The issue that I'm having with bottle is getting the image-generating script to execute when it receives the post request. I'm used to handling the post request and callback with javascript (or jquery). should I be using a bottle method instead?
I actually resolved the issue. The Bottle framework tutorial encourages first-time users to set up the server on a high port (to avoid conflict with apache, etc) for development. I was missing two parts of the process: 1. import the python script so that it can be called from the main bottle file 2. in the main bottle file, add a route to the api link (for the javascript to work) I'm not sure if I would have had to add the route if I was running the server on port 80

How can I route HTTP requests through a python script?

I've thoroughly searched for the answer for my question, but I don't think I know the correct terminology to make my searches effective. Anyway, I'm looking to set up my web server to route a request for a static page through a Python "PageBuilder" program which will dynamically implant content into templates.
In other words:
user requests mysite.com/index.html
server passes request to certain python script (my server is currently Apache2)
python script generates output, passes output to server
server serves content to user
Could someone please help me or at least give me the "proper terminology" with which to search?
This isn't really static content any more. You want to dynamically generate content using a script.
In Python, this is generally done using the WSGI interface (in olden days, the de facto standard was CGI), which defines a standard way for your Python code to get input (information about the HTTP request), and send back a response (HTTP headers, and the content you want to serve -- typically HTML).
To this end, there's the excellent mod_wsgi Apache module that sets up the plumbing between the server-stuff (receiving requests and sending back content) and your code, using this WSGI interface.
You may also want to check out some sort of templating engine to make generating dynamic HTML easier (my favourite is Jinja2).
This is called CGI.
http://en.wikipedia.org/wiki/Common_Gateway_Interface

Categories