Stop Python localhost server on every port - python

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.

Related

I've created a simple .onion site, but instead of the Tor browser displaying my index.html file, it displays my folders

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

Raspberry Pi SimpleHTTPServer startup on boot with chromium launch

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

Getting Terminal to do something else while running a localhost on it.

How come I can't do anything else on terminal when It's running a localhost. Like I run a google app engine on it and make a new local host, I for example try to see what directory I'm in or even change directories, it doesn't respond to any of my requests? Thank you
That instance of the terminal will be busy doing the thing you asked it to. But you can just open a new window or tab.

Google app engine endpoints api not working in localhost

I am trying to start a python app-engine project on localhost. I am in a Windows 8.1 machine (Python version 2.7). The project code is copied from here link. The server seems to start but the page wouldn't properly load due to some problem. Probably it can't reach the api server.
Here is a shot from firebug showing the failed request
What seems off to me is that in command prompt (I started the dev server from there) the API server is started at some random port other than the module port. But the webpage is trying to access the http://127.0.0.1:8000/_ah/api/static/proxy.html... page at the module port. I tried accessing it at the API server port it does open some page there. (I mean I tried opening http://127.0.0.1:1532/_ah/api/static/proxy.html... seperately) It returns some json I don't understand...
{app_id: dev~udatut-bs, rtok: '0'}
Command prompt log:
Here's the difference between the outputs in localhost and *.appspot.com
Should get this:
But stuck here:(check buttons are not loaded)
I finally found a solution here.
Steps for windows:
Goto your google appengine installation folder and then ./google/appengine/toots. (C:\Program Files (x86)\Google\google_appengine\google\appengine\tools) if you followed default installation instructions.
Open the file appengine_rpc.py and look for the line (towards the end of the file)
opener.add_handler(fancy_urllib.FancyProxyHandler())
and comment it out like
# opener.add_handler(fancy_urllib.FancyProxyHandler())
Close the app and restart it again.

How can I create an local webserver for my python scripts?

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!

Categories

Resources