Run a python program from browser - python

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.

Related

I want to implement a Python chatbot on my static HTML website

I am trying to implement a chatbot that I created in Python on my website. I have looked at so many tutorials and most of them uses some sort of SocketIO connection which they use on localhost.
My current setup
I have a static website made with HTML, CSS and JS which is hosted on GitHub Pages (for now). As a separate project I have a Python project in which I have a function that can take an input message and then return the "correct" bot message from that.
What I want
I now want to create a html interface where a user can input his message and press send. Then (in JS I assume) I will take this message, somehow send it to the python function, get a response from the python function, and then print it out in JS. No other users should see these messages, just the current user.
My problem
I know the JS steps but I have no clue how to make a JS function "talk" to a python function/script/project and then use the response from that. I have seen so many tutorials on flask/socketio but all seems to be targeted to localhost and some sort of live chat between users.

Python and Selenium - Reboot program and reuse same browser session

Scenario:
I am working an an auto whatsapp responder using whatsapp web.
I log in via chromedriver on selenium with python 3.
I run a function that does some stuff inside a while True.
Problem:
Sometimes, due to a lack of conectivity with the phone, or whatever other problems, the program just does not keep running the right way.
There are a lot of factors that might cause the whole thing to lose the right flow. I am analyzing them all and fixing them as best as I can.
Question:
I came up with the idea that maybe if I restart the whole thing every hour (or every whatever-thousands iterations) it would become more solid. As it will refind the flow no matter what happens, if I did not catch the bug yet.
Is it possible to restart the whole thing, without losing the browser session? Whatsapp web requires a QR scan, but it allows a "keep session alive in further connections" (which I do not really know how it works... if cookies or something else.)
Note: I know that a python script can be rebooted, but the bigger problem here is to reuse the browser session. Of course I am doing my research. None of what I read so far made me come with a solid solution, and that is why I ask to all the super cool brains out there.
Whatsapp stores session in localStorage of the browser.
You can extract the localStorage and save to a file on closing of a session.
Upon instantiating a session check if this file exists, then parse the file and update localStorage with saved values before opening a URL.

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.

How can I make Python's 'webbrowser' block execution?

I want to implement a Python script to act as my OAuth2 endpoint, since I'm trying to write a Soundcloud app. Part of the authentication process involves visiting a Soundcloud page where you can sign in and grant access to the given Soundcloud application.
I'd like to be able to open that webpage in a browser using Python 3, which you can do with the webbrowser object. You can see on the documentation that launching a text-based browser blocks execution; I want to block execution whilst the webpage is open in a GUI-based browser.
Does anyone know whether this is possible?
This might be impossible to do portably. For example, if Firefox is already running on Linux, the second invocation of firefox http://url will find out that an instance using the same profile, will send a message to the other process to open that URL in a tab, then exits immediately.
However, you could accomplish the same thing by sending the authentication tokens to a server, and simultaneously polling the server for credentials in the python script.

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