I am currently trying to get a display window I made in html setup to work immediately when the pi is plugged in, without needing to type in command prompt codes everytime it is shutdown
The 3 commands I need are:
cd ~/Desktop/screen
python -m SimpleHTTPServer
chromium-browser --start-fullscreen http://localhost:8000
The first one directs to the location the files are in (index.html
and its supporters)
The second one starts the server in that location
The third one launches chrome in f11 mode, filling the entire screen and connected to the default location of SimpleHTTPServer
I have implemented this directly in the terminal and it works, but even just sticking this in a .sh file does not operate, let alone hooking the .sh into the backend. I was following this instructable: https://www.instructables.com/id/Raspberry-Pi-Launch-Python-script-on-startup/
The thing is only serving static content, and people only will access it with their eyes looking at the screen. (If chrome allowed access to local files, it would literally be opening an html document). I tried setting up a full server, but between me being new to linux, pi, and servers, that did not get far.
Any help getting this set up will be greatly appreciated.
I got it working. There needs to be an '&' at the end of SimpleHTTPServer
python -m SimpleHTTPServer &
Also, chromium cannot be booted at this point, as the screen has not been initialized by the pi yet (from what I read). Instead, go into chromium's settings and change the startup page to "localhost:8000"
final .sh file
#!/bin/sh
#!launcher.sh
#!navigate to home directory, then to here
#!launch simple python server
#!open chrome at localhost:8000
cd /
cd /home/pi/Desktop/screen
python -m SimpleHTTPServer &
cd /
you will need to open chrome manually, but upon opening, it will be on the correct page.
The rest of the instructions at https://www.instructables.com/id/Raspberry-Pi-Launch-Python-script-on-startup/ worked
Related
This is my first question so please bear with me. I looked for answers to my question, but found none unique to my issue.
I recently tried my hand at hosting an onion site with Python3 and the Tor browser. I created a directory called "tor_service" and placed a simple index.html file inside.
I then ran python3 -m http.server --bind 127.0.0.1 8080 in the same folder. Next, I opened another terminal session and started the Tor client. I navigated to, and appropriately modified the torrc file under the "hidden services" section, using port 8080. I opened my browser and went to localhost:8080 to verify everything was working. To my surprise, I saw the simple web page I wrote. So far, so good.
I then navigated to the Tor folder that contained the hostname to get the unique oinion address, and using cat hostname, obtained the string of characters followed by .onion.
I pasted the onion address in the Tor browser and again, I saw the simple html page I wrote.
Here's the problem: when I shut down my computer for the night, resumed the next day, and started the Python server and Tor client, instead of displaying my web page, I got a page that read "Directory of /" that displayed all of the folders on my hard drive. This is obviously a serious security issue. Not only were the folders on my hard drive on display for the web to see, they were all hyperlinks.
I don't understand what I did wrong. I didn't modify or move any files or change any settings, and I started the Python http.server and Tor client as usual.
I closed the terminal windows opened new ones, but got the same results. A third time resulted in the same outcome. I understand that using the Python "server" isn't the optimal method to host a site, but if I can't get something so simple to work, I fear I won't be able to actually write (much less diagnose problems on) a more robust server that operates using more than a single line of code.
I'm using the Linux subsystem on Windows 10 and the latest iteration of Ubuntu (20.4, I think).
Any pointers are greatly appreciated. I can craft and style a nice looking webpage, but I'd like to get proficient at the back-end side of things.
Are you sure you have navigated back to the project folder in your terminal?
If you're running the
python3 -m http.server --bind 127.0.0.1 8080
command immediately after opening the terminal, it will run in the 'default' folder rather than your project folder (you'll need to 'move' back into the project folder using cd).
Since I tried a tutorial with Python / Django using venv etc, i'm blocked with a localhost server that shows folder of this project:
I use Visual Studio Code and I use LiveServer configured on port 0. Each time I want to start LiveServer, it renders me this page and nothing else.
Have you any idea why and how to stop it?
I modified my PATH folder to make the Python's project work. Is it the problem?
Thank you
The way to fix this is by going to VS Code >> File >> Open Folder, and the select the folder with your html file in it then you can run live server properly. However, if you want to stop live server simply closing the web browser it's running on will do.
I am leading a team of analysts and want to introduce them to Jupyter Notebook as a window into Python programming.
We have Anaconda downloaded and installed on our Linux server. I've asked our IT to help set it up to run on Google Chrome and they have been able to only provide the following steps:
source /R_Data/anaconda3/etc/profile.d/conda.sh
this kicks off Anaconda on the server, must run in PUTTY. We stored the installation in the same location as RStudio hence the R_Data in the filepath.
/R_Data/anaconda3/bin/jupyter-notebook --ip 0.0.0.0 --port 8889
This sets up the port 8889 with a token generated each time from scratch. We then need to grab the token id and paste into Chrome with the full URL per step 3
http://localhost:8889/?token=ea97e502a7f45d....
When I paste this in Chrome it loads Jupyter.
While this gets the job done it seems less than ideal for an entire team of analysts to have to do this each time. We also have RStudio installed on the same server but that simply opens from Chrome using a URL since I assume it is always running in the background. Jupyter and Anaconda seem to only run once they are kicked off first in PUTTY and I would like a way to bypass those steps.
I am familiar with the Jupyter config file however my limited understanding as a non-developer tells me it applies only to each user and cannot be applied to all users simultaneously (i.e. as a root user on the server or something to that effect).
I am hoping someone here might point me in the right direction. I should also point out that as a Redhat user I can't follow instructions based in Ubuntu since that syntax seems different.
Many thanks for the help.
Yoni
A convenient way is to run jupyter notebook --no-browser --port=12345 on your server while connecting through the ssh tunel as ssh -N -f -L 12345:localhost:12345 myserveralias. Now jupyter is on your 12345 localhost. Things like AutoSSH or Keep Alive will help with an erratic network, however, take security into account.
I have the following situation.
I want to write python code in my Laptop that will take more than 24 hours to run.I am using UBUNTU 12.04 lts.
Is it possible so that I write python code in my laptop, automatically send it to some remote desktop, run there and send the output result to my laptop when done?
one way suggested to me is to use openssh.
But I want to do this in the following way----
Write and Debug Python Code in my laptop.(Solved)
email the code as attachment to ****#gmail.com(Solved)
Other python program in the desktop will automatically download and run the source code(Unsolved)
and email the output file back to my gmail id.(Solved)
what is the python code to download the attachment from the latest email from a specific gmail folder?
If your remote system is windows, a good option would be to use PsExec from SysInternals.
Ex. If your script is long_running.py a typical usage would be
PsExec \\remote-server -c long_running.py
If your remote system is *nix, and your local system is Windows, you can use ssh for remote execution via Plink (part of PuTTY).
plink remote-server#user -m long_running.py
Finally if both remote and local machine are *nix systems, you can simply use ssh
ssh remote-server#user 'bash -s' < long_running.py
Note This is just some possible options, but the idea is remote execution is possible either via ssh or a similar option (like PsExec) for Windows
If both systems are running *nix, you can easily do all your dev work and debugging locally, while still executing remotely:
One time set up:
Mount a folder from the remote box locally
On your laptop, save your project/script to that (now local) folder, or set the mounted folder as your project's save path in your IDE.
Publishing:
Do work
Click the save button
Executing:
SSH into the remote box and open a new screen
Navigate to the folder you'd previously mounted, and run your script.
You can then safely detach and close ssh if necessary (ctrl+a d), and re-attach later:
3a. screen -ls (to find the screen name)
3b. screen -x screen_name
The advantage of this solution is that if you've got an ongoing project requiring frequent edits/changes, you can do all your dev work/debugging locally, and the only work required to "publish" is clicking the save button, starting the screen, and running.
I'm looking to use a local webserver to run a series of python scripts for the user. For various unavoidable reasons, the python script must run locally, not on a server. As a result, I'll be using HTML+browser as the UI, which I'm comfortable with, for the front end.
I've been looking, therefore, for a lightweight web server that can execute python scripts, sitting in the background on a machine, ideally as a Windows service. Security and extensibility are not high priorities as it's all running internally on a small network.
Should I run a native python webserver as a Windows service (in which case, how)? Or is it just as easy to install Apache onto the user's machine and run as CGI? Since this is all local, performance is not an issue either.
Or am I missing something obvious?
Don't waste a lot of time creating Windows service.
Don't waste a lot of time on Windows Apache.
Just make a Python service that responds to HTTP requests.
Look at https://docs.python.org/2/library/basehttpserver.html
https://docs.python.org/3/library/http.server.html for version 3
Python offers an HTTP server that you can extend with your server-side methods.
Look at http://docs.python.org/library/wsgiref.html
Python offers a WSGI reference implementation that makes your server easy and standards-compliant.
Also http://fragments.turtlemeat.com/pythonwebserver.php
"I'm trying to avoid making the user run python stuff from the command prompt."
I don't see how clicking a web page is any different from clicking desktop icons.
Starting a web server based on Python is relatively easy, once you have the web server. First, build the server. Later, you can make sure the server starts. Let's look at some ways.
Your user can't use a random browser to open your local page. They need a bookmark to launch "localhost:8000/myspecialserverinsteadofthedestop/" That bookmark can be a .BAT file that (1) runs the server, (2) runs firefox with the proper initial URL.
You can put the server in the user's start-this menu.
You can make your Python program a windows "service".
Best way is to make your own local server by using command prompt.
Make a new folder say Project
Make a new folder inside project & name it as "cgi-bin"(without quotes)
Paste your .py file inside the cgi-bin folder
Open cmd and change to the directory from which you want to run the server and type "python -m CGIHTTPServer"(without quotes)
Minimize the cmd window & open your browser and type "localhost:8000/cgi-bin/yourpythonfilename.py"(without quotes).
The wasiest step would be navigate to folder where your files are located and running http.server module
cd /yourapp
python3 -m http.server
the you should see something like this in console
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
Running a native python webserver as a windows service should be a no brainer. Check out the documentation for writing windows services (win32api, ActiveState python) in python and also the documentation for subclassing BaseHttpServer and SimpleHttpServer.
BTW: I had a similar question on stackoverflow: How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?
Basically, you subclass BaseHTTPServer (you have to anyway...) and then... but just read the accepted answer - it set me on the right track!