Situation: Need 2 ad-hoc Python3 http.server instances on the same computer.
Problem: The first server was started successfully on the command line
python3 -m http.server 8888
The second server was attempted with the following script:
import http.server
import socketserver as ss
os.chdir("/path/to/working/directory")
Handler = http.server.SimpleHTTPRequestHandler
with ss.TCPServer(("", 8000), Handler) as httpd:
try:
httpd.serve_forever()
except PermissionError:
print("Permission denied.")
The second server terminated with OSError: [Errno 98] Address already in use.
Question: How can I run two Python3 http.server on the same machine (listening on 0.0.0.0)?
Additional Information 1: I have checked, and there are no other services holding onto port 8888 (server 1's port), and 8000 (server 2's port).
Additional Information 2: I am not sure why, but if I reverse the two ports, both servers run as intended (i.e. server 1 runs on port 8000; server 2 runs on 8888). Any ideas why?
Related
when using the command
python -m Http.server 8000 --bind 192.168.1.105
the server serves the localhost only even after binding with the device address
I've tried binding with 0.0.0.0
python -m Http.server 8000 --bind 0.0.0.0
but nothing changed
and there is no VPN running on the device
I'm working on windows platform and trying to access the server form a mobile phone and a Linux platform
I keep getting a message from the client's device browsers that say the address is unreachable
I usually start http.server by typing
python3 -m http.server port and I can access the server by going to localhost:port
My goal is to access the srver by simply typing localhost.
There is always port... the default is 80, so just run it on 80 and you'll reach it by just localhost.
I started working on a simple server and client script. I tested the script on my local network, and it worked great: The server would turn on, and wait for a client connection. As soon as a client connected, it would then let me proceed.
I then decided to test it over the internet, and this is were the problems start happening. I am running the server on Ubuntu, and the client on a windows machine.
Server Connection Code:
import socket
import sys
#Create a socket for connection
def socket_create():
try:
global host
global port
global s
host = ''
port = 5698
s = socket.socket()
print("Socket created.")
except socket.error as msg:
print("Socket creation error: " + str(msg))
#Bind the created socket to a port, sleep for conn
def socket_bind():
try:
global host
global port
global s
s.bind((host, port))
print("Waiting for connection")
s.listen(5)
except socket.error as msg:
print("SOcket binding error: " + str(msg) + "\n Retrying...")
socket_bind()
#Estabilish Connection with client
def socket_accept():
conn, adress = s.accept()
print("Connection has been estabilished | " + "IP " + adress[0] + " | Port " + str(adress[1]))
send_command(conn)
conn.close()
Client Connection Code:
import os
import socket
import subprocess
# Create a socket
def socket_create():
try:
global host
global port
global s
host = 'My Internet IP'
port = 5987
s = socket.socket()
except socket.error as msg:
print("Socket creation error: " + str(msg))
# Connect to a remote socket
def socket_connect():
try:
global host
global port
global s
s.connect((host, port))
except socket.error as msg:
print("Socket connection error: " + str(msg))
And the error:
[Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I'll never get the "Connection has been estabilished" print from the server, and I'll get that error.
Can anyone see anything that might be causing the problem in the code?
After port forwarding the router, do I have to do any other configs on Ubuntu itself? The ports I should open are as TCP, right?
After opening the port on the router, and if I use a service like "http://www.canyouseeme.org/", will it show instantly that the port is open, or will it only show if I'm running the server and waiting for a connection?
I managed to fix the problem. Here is an in-depth guide on how I did it.
The problem: Even after opening a port on your Router configs, you still can't see the port open on your running service.
The solution: Port Mapper.
Things to note: I had to run Port Mapper on Ubuntu, because running it on Windows didn't seem to work for me. Also, if you let your computer sleep or shutdown, when you turn it on again, you'll have to reopen the ports (but don't worry, as it is just a click of a button).
What you'll need: https://sourceforge.net/projects/upnp-portmapper/
First, simply run 'java' in the terminal to make sure you have Java installed, or in order to install it (directions will appear on screen).
From the given link, download the Portmapper.jar.
After downloading it, simply run 'java -jar Portmapper.jar' on the terminal to open up the gui.
After opening the gui, press Connect so you can automatically connect to the router.
All the current open ports will now appear on screen. We know want to look for the port mapping presets.
In the Port mapping presets, go ahead and press Create.
Here, give the preset a name. Then, fill the Remote Host if you want the connection of a specific IP, or leave it empty for any IP. The internal client will be your Server's network IP (in my case, because I'm running the server in the same machine as the Port mapper, I'll tick Use Local Host.
Now we'll go ahead and add a new port as a TCP connection. Here we can either have the external and internal ports with equal or different values. Just remember the internal port (your machine's port) will be the one you'll use on your server, and the external port (your router open port) will be the one you'll use on your clients or whatever you are connecting to your server.
After this, simply save the preset, choose it and press Use. If you know click Update under the ports list, you'll see your new open port. Just to make sure, you can get your server running awating connections, and simply go to "http://www.canyouseeme.org/", input the port, and here you go.
Do remember that after shutting down or putting the computer asleep, you'll have to go back to PortMapper, and click Use on the preset you want again (depending on what port you want).
I am trying to run a server in BeagleBone Black using CherryPy and following this tutorial http://docs.cherrypy.org/en/latest/install.html, and every time I run it I have this error message
ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(( '127.0.0.1',8080))already shut down
How can I turn it on again ?
I tkink the port 8080 is already used. Your command is fine to kill a process running on particular port on Linux.
fuser -k 8080/tcp
If you try to access your device remotely you have to explicitly configure Cherrypy to bind on all interfaces:
import cherrypy
class HelloWorld(object):
#cherrypy.expose
def index(self):
return "Hello world!"
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '0.0.0.0'} )
cherrypy.quickstart(HelloWorld())
I am running CherryPy as a webserver on a remote Linux machine. End users access a website over the internet which the CherryPy instance serves. So far, so good.
Now, I want to have a dev version of the site, running on the same machine but on a different port, so that I can develop and test without disturbing the prod website. I need to do development on the same machine because it contains a lot of data which would not be practical to move to a different machine.
The code for the dev instance is in a different directory, of course, so as I edit the dev code it does not trigger a reload for the prod instance or affect the prod instance in any way.
However, if I configure my dev instance to run on a different port than the prod instance and try to start up the dev instance, I get:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/cherrypy/process/wspbus.py", line 197, in publish
output.append(listener(*args, **kwargs))
File "/usr/lib/python2.7/dist-packages/cherrypy/_cpserver.py", line 151, in start
ServerAdapter.start(self)
File "/usr/lib/python2.7/dist-packages/cherrypy/process/servers.py", line 167, in start
wait_for_free_port(*self.bind_addr)
File "/usr/lib/python2.7/dist-packages/cherrypy/process/servers.py", line 410, in wait_for_free_port
raise IOError("Port %r not free on %r" % (port, host))
IOError: Port 8080 not free on '127.0.0.1'
Neither the dev nor prod instance are running on port 8080. The prod instance is on SSL (443) and the dev instance I can put on an arbitrary port (let's suppose it's on 98765). I haven't specified 8080 anywhere in any config file. So I didn't expect there to be a port conflict, However, it appears that for one instance, CherryPy spawns 2 processes- I guess there is some sort of parent-child relationship- and one of the processes is somehow listening on localhost:8080. Because this happens for both instances I try to launch, the second one is unable to launch because there ends up being this conflict on 8080.
I have tried several things, such as turning off autoreloading and timeout monitoring, in the hopes that CherryPy would not launch the process associated with 8080, but no luck so far.
Thanks in advance for any help!
I had the same issue, to fix add this to each cherrypy
cherrypy.config.update({'server.socket_host': '127.0.0.1',
'server.socket_port': 9023
})