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

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.

Related

Run a python program from browser

I want to run a python program (kinda like this) from a browser
Anyway, as you see it has a few inputs, and i would like to "translate" that into a kind of form
def addNew():
appendBase = open('dBase.cfg','a')
uname = input('Username: ')
pword = input('Password: ')
appendBase.write(uname+','+pword+'\n')
print('\nAdded new profile: \n'+uname+','+pword)
appendBase.close()
Also i dont know how to get the print to the page, so it can show it
I've just started learning, so go easy on me, please
It is not possible to actually run this in the browser, for various reasons.
you can't run python in browsers. only javascript
you can't open local files from a browser
there's no command line to input from some terminal
Most things you see on the web have two parts
a part that actually runs in the browser. Written in HTML and javascript
another part where the browser connects, to send and receive data. That can be done in any language, including python. However, that part is not visible in the browser
The two parts communicate using HTTP protocol. So, start by reading a bit on HTML/javascript (W3Schools is an easy way to get started). When you feel comfortable with that, practice with a python web framework (django is the most popular, but flask is the easiest to get started), and see how javascript uses HTTP to connect to that.

invoke python script and populate results on webpage

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.

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.

does the function in a python program written using sched, impact/block the the other functionality in the program?

I am new to the programming world and trying out something with Python.
My requirement is to have http web server(built using BaseHTTPServer) that runs forever, which takes an input binary file through HTML form based on user selection and returns a set of HTML files back on the web client.
As part of this when the user is selecting his specific input file, there are set of folders created with HTML files written inside those folders in the server, i thought of putting in a tidy up functionality for these folders on the server. So that everyday the tidy up would clean up the folders automatically based on a configuration.
i could build both these modules in my script(http web service & tidy up on server), specifically the tidy up part is achieved using python's sched module
Both of these functionalities are working independently, i.e
when i comment out the function for tidy up, i can access the server url in the browser and the index.html page shows up correctly and further(accepts binary, parsing happens and output htmls are returned)
when i comment out the function for http server, based on the configuration set, i am able to ensure the tidy up functionality is working
But when i have both these functions in place, i see that the tidy up function works/is invoked correctly for the scheduled time, but the index.html page is not loaded when i request for the server on the browser
I researched on the sched module enough to understand that it is just to schedule multiple events on the system by setting time delays and priorities
Not able to work both the functionality
Questions:
Is this a correct approach, using sched to achieve the tidy up?
If yes, what could be the reason that the http service functionality is blocked and only the tidy up is working?
Any advice would be helpful. Thanks
For now, changed the function call for the tidy up feature, by using the background scheduler implementation of the APScheduler module of python.
This does not impact the function for serving http requests and has currently solved my problem

how to invoke python script in jsp/servlet?

I am trying to invoke a python code for screen scraping (using Beautiful Soup) from my jsp servlet. Or it would also work if it can be directly invoked from the HTML.
Looked through few threads but couldn't get any solution.
What I want is to give the python program some arguments and want it to do some screen scrapping and return the result to jsp somehow.
I assume you are talking about web scraping which is pulling information from other websites.
You are not going to be able to do something like this in somebody's browser because it violates Javascript's same origin policy, AND there is no way a browser is going to let you download and execute a script on a client's computer.
You could just write a python script to do this for you and execute it yourself on your machine however.
Just be sure you are not violating web site's terms of service.
EDIT:
In that case I would recommend running the script on the command line, and then using the output of the program in the servlet to generate the responses you want.

Categories