Cannot create local server HTTP port with python in Pycharm - python

I am trying to run the python .py file in PyScript. To do that, I need to load python file in a server because PyScript cannot access local file:
<py-script src="./greet.py"></py-script>
So I tried to create local server by entering this command in the terminal in the folder of my project:
python -m http.server
It has been over 30 minutes now and the server is not on. It shows that the terminal local is still running. It should take no more than a minute to be done. I have tried to locate specific port with:
python -m http.server 80 but it still doesn't work.
What should I do?

You might try also binding the server to a specific link-local IP address, using something like python -m http.server --bind 127.0.0.1 8000. The page should then appear at 127.0.0.1:8000.
This was necessary for me on Windows - even after allowing Python permissions to access the network. Not sure if it's Windows-related, or Python not identifying the correct NIC to bind to, or what.

Related

server program exits when i close SSH connection to GCP

I have an Ubuntu instance on Google Cloud Platform (GCP). I want to use it as an HTTP server to access files. I simply use this python command, type it in bash:
python3 -m http.server 8000
This will run http.server module as a script, construct a simple HTTP server and listen at port 8000.
Problem is that, since I use GCP instance, I must connect to it remotely (for example I use SSH shell provided by GCP). When I close the SSH shell, the python HTTP server will stop. So what should I do to make sure that the server still runs after I close the shell?
I did searched on Google, and I tried to use
nohup python3 -m http.server 8000 &
This command, I quote, will run the instruction as a background program and persist running after exiting bash. But it seems that this doesn't work for my situation.
Anybody can help?
Try the screen command. I think it's easier to use and also more flexible than nohup as you can also reattach processes after detaching then. See this answer for details.
The http.server module is not meant to be a full-fledged webserver.
You'll want to set up something like Apache instead, see Running a basic Apache web server.

127.0.0.1 refused to connect

On command prompt I typed
python -m HTTPServer
and I got
Serving HTTP on 0.0.0.0 port 8000
But when I try to access the localhost page it just won't open.
I also tried
http://127.0.0.1:8000/
but still, nothing opens up.
output:
Also, my pc have anaconda installed and I think that it may be hindering with 8000 port. I don't want to uninstall anaconda. How can I use this port or another new port as I m trying to learn D3?
Using python3, run the following command in the directory of your web assets:
python -m http.server 8000
This should be equivalent to using SimpleHTTPServer in python2.

Access SimpleHTTPServer from outside Network

Apache server can be set up and accessed from ouside Network in the following way:
http://lifehacker.com/124212/geek-to-live--how-to-set-up-a-personal-home-web-server
I want to achieve similar functionality with python SimpleHTTPServer.
How is this possible?
Step 1: Run this command "python -m SimpleHTTPServer". Note that python -m SimpleHTTPServer works only with python 2. With python 3, you should use: python -m http.server
Step 2: Edit your router's configuration to forward port 8000 to the computer on which you ran the python command.
Step 3: Determine your home network's IP address, for example, 203.0.113.47
One convenient way to determine your home network's IP address is to consult any of the what-is-my-ip websites, for example https://www.whatismyip.com/.
Step 4: From outside your network, visit (for example) http://203.0.113.47:8000/
In case the port 8000 is blocked in your firewall, you have to open it.
For example, on RHEL/CentOS/Fedora, open port 8000 as shown below.
#firewall-cmd --permanent --add-port=8000/tcp
#firewall-cmd --reload
On Debian, Ubuntu you can allow the port as shown below.
$ sudo ufw allow 8000
You can either port forward your router to the specific port and use a dynamic DNS service such as no-ip to reach your server.
Or you could use a tunneling software like localtunnel.me or ngrok to forward your port to a unique URL.
Or you could create a reverse proxy or a VPN server if you own a cloud server such as AWS or DigitalOcean.
Yeah, localtunnel helps you in external access of python http.server.
Here external access means - to access your http.server from outside the network (suppose you're in Delhi running python http.server and want your friends who are in Bangalore to see the directory contents).
Requirements:
nvm (for installing localtunnel)
use 'lt' (CLI tool) on a specific port (but remember, in order to make external access on your python http.server you need to run python -m http.server along with )

Change directory Python SimpleHTTPServer uses

Running a basic Python SimpleHTTPServer to check out some files in browser. Once the SimpleHTTPServer is running in one directory how do you stop it and use a different directory? or just have it switch to the new one.
Currently using in terminal:
python -m SimpleHTTPServer 8008
Then if I try to run on another directory it says it's already in use. So basically how to I stop a given instance of SimpleHTTPServer?
Then if I try to run on another directory it says it's already in use
You can only bind to each port once with SimpleHTTPServer. If you are already running the server on port 8008, you can not run another instance listening on that same port.
you should stop the server, and launch it from the other directory... or choose a different port number for running the 2nd instance.

python -m SimpleHTTPServer - Listening on 0.0.0.0:8000 but http://0.0.0.0:8000/test.html gives "Page Not Found"

After cding to my folder I enter
python -m SimpleHTTPServer
and get
Serving HTTP on 0.0.0.0 port 8000 ...
in reply. But when I hit http://0.0.0.0:8000/test.html I get a page not found error.
I've also tried
pushd /path/you/want/to/serve; python -m SimpleHTTPServer; popd
taken from this question
When I hit ls I can see the file and the directory. Anyone know what I'm doing wrong?
I think the other two answers are trying to make it clear that 0.0.0.0 is not the URL you should be visiting. When a Python web server (like cherrypy for instance) says it is serving on 0.0.0.0 it means it is listening for all TCP traffic that ends up at that machine no matter the hostname or IP that was requested. But, if you change it such that the socket listens on 127.0.0.1 or 'localhost', then unless the request was specifically to that IP/hostname, it won't respond to the request. For example, many times you can use your machine name instead of localhost (ubuntu allows this for example). If your machine name is 'brian' and you have a server listening on 0.0.0.0:8080, you should be able to reach that server with http://brian:8080. But if that server is listening on 'localhost', even though 'brian' is set to point to 'localhost', the server won't receive the message.
You also need to be sure the file really is in the directory you are running the command from. Otherwise, the 404 response is actually correct :)
Good luck!
Have you tried http://127.0.0.1:8000/ ?
:)
You must type in the ip-address of the computer your connecting to for example 192.168.0.2:8000 Change that to the ip-address of your server.
Try browsing to http://localhost:8000/test.html or http://127.0.0.1:8000/test.html (those two should be exactly the same thing as long as your hosts file isn't all crazy-like).
0.0.0.0 is usually used by Windows as the "Not connected" IP, and can also be used as a sort of wildcard for when dealing with IPs. I am a bit confused at why your HTTP server is trying to host on 0.0.0.0, though. You may need to edit some config files and set that to 'localhost' or '127.0.0.1'.
Try to host over localhost may it help you instead of trying it on http://0.0.0.0/ like this way: python -m http.server 8000 --bind 127.0.0.1
create a directory e.g. mkdir HTTPServer_dir
move inside the folder cd HTTPServer_dir
typing the command (according to python version) python -m SimpleHTTPSever 8000
(or the port you want)
go on a browser and type http://127.0.0.1:8000/
Done !!!
You could make a simple index.html page inside the HTTPServer_dir so you can see an html page instead of directory listing
Run ifconfig on Linux or ipconfig on Windows to find the ip address of the server.
$ sudo ifconfig
wlan0 Link encap:Ethernet HWaddr 30:3a:64:b3:be:6a
inet addr:192.168.1.103 Bcast:192.168.1.255 Mask:255.255.255.0
Here in case the url would be:
http://192.168.1.103:8000/test.html
try this in python3
python -m http.server 8000 --bind 127.0.0.1
and in your browser this url:
http://127.0.0.1:8000/
Sometimes the same port number is used by some other service. So we can try with some other port like
python -m SimpleHTTPServer 9090
And then simply hit http://{your system IP}:9090/
this works for me.
this worked for me,replacing your machine name with
http://localhost:x000
This worked for me on Windows 8. Did not download any software!
In cmd:
Go to the directory that your file is in.
python -m SimpleHTTPServer
Shows "Serving HTTP on 0.0.0.0 port 8000 ..."
Now, find out your system name. For Windows 8: Control Panel -> System. You will see the computer name here. Let's say it is "Abhinav".
Your local server will be hosted at "Abhinav.local:8000".

Categories

Resources