I am trying to set up a simple web service but when I do an HTTP request to the port 8080 of my server nothing happens...
I have discovered that the simple python server that i have set up listens on the 8080 port of the primary private IP and not on the public IP port.
How can I send the HTTP requests to the python script? Do I have to NAT?
I am on ubuntu 14.04 server
This is the simple python web server
from BaseHTTPServer import BaseHTTPRequestHandler
import urlparse
class GetHandler(BaseHTTPRequestHandler):
def do_GET(self):
parsed_path = urlparse.urlparse(self.path)
self.wfile.write(parsed_path.query[2:])
return
if __name__ == '__main__':
from BaseHTTPServer import HTTPServer
server = HTTPServer(("0.0.0.0", 8080), GetHandler)
print 'Starting server, use <Ctrl-C> to stop'
server.serve_forever()
Change the line:
server = HTTPServer(("0.0.0.0", 8080), GetHandler)
To use the IP address of the public IP network adapter instead of "0.0.0.0"
The problem was that I didn't set a rule for port 8080! Thank you to everybody!
Related
I'm doing some penetration testing and I want to try something in python that I haven't clearly found on the internet. I want to connect port 80 of my website to my own web server so that my website can pull its source code from my web server instead of through its normal web server. Do you get what I mean?
I just want to basically connect my website to my web server so that the source code on my web server will load on my website instead of the normal way where my website pulls its source code from its own web server.
My code is:
import socket
from http.server import HTTPServer, BaseHTTPRequestHandler
HOST = 'www.serviceonefinancial.com'
PORT = 80
socket = socket.socket()
socket.connect((HOST, PORT))
socket.send(b"""
GET / HTTP/1.1
Host: chase.com
""")
msg = socket.recv(1024)
print(msg)
host1 = "192.168.0.206"
port1 = 9999
class NeuralHTTP(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><body><h1>HELLO WORLD!</h1></body></html>","utf-8"))
server = HTTPServer((host1,port1), NeuralHTTP)
print("server now running!")
server.serve_forever()
I basically created a socket connection into port 80 and a web server but I haven't found a way to connect them yet. I want to connect my web server to port 80 through some kind of connection so that they can communicate and the web page for my web server will load on my website.
my python codes
# _*_ coding:utf-8 _*_
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import SocketServer
class testHTTPSERVER_RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write("<html><body><h1>Hello world</h1></body></html>")
def run(server_class=HTTPServer,handler_class=BaseHTTPRequestHandler):
print('starting server ....')
server_address = ('127.0.0.1',8081)
httpd = server_class(server_address,testHTTPSERVER_RequestHandler)
print('running server')
httpd.serve_forever()
run()
and docker file
FROM python:2.7-onbuild
#python:2.7.13-alpine dene
ADD testhttp_server.py /src/app/testhttp_server.py
WORKDIR /src/app/
EXPOSE 8081
CMD ["python","testhttp_server.py"]
docker run and logs
mozilla
so where is my mistake??I'm working on that during two days but I didn't find anything else
Change this
server_address = ('127.0.0.1',8081)
to
server_address = ('0.0.0.0',8081)
Listening to 127.0.0.1 inside docker container means you want to listen to traffic generated from inside the container only but when you map a host to container. It is sending the traffic from host to the IP of the container. Which id dynamic and not know before hand. So you need to listen to all interfaces inside the container. And that is why you should use 0.0.0.0
I want to launch Python HTTPServer on heroku. Note that this is no Python framework. The code snippet is attached below. How will I be able to launch this server on Heroku? I am able to run this server on my local machine. But I want it deployed on Heroku. Please provide insights.
Server Code:
import http.server
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver
import threading
PORT = 5001
class myHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.write("Heroku is awesome")
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
try:
server = ThreadedTCPServer(('', PORT), myHandler)
print ('Started httpserver on port ' , PORT)
ip,port = server.server_address
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()
allow_reuse_address = True
server.serve_forever()
except KeyboardInterrupt:
print ('CTRL + C RECEIVED - Shutting down the REST server')
server.socket.close()
When heroku runs your process, it defines the environment variable PORT to the internal port you should expose your server on. Your server will then be accessible from the internet on port 80, the default HTTP port.
Python can access environment variables with os.environ.
So you can use:
PORT = environ['PORT']
os.envron docs here
You can read more about how Heroku handles ports here.
Create a Procfile with a single line:
web: python yourscript.py
I build a socket server with python's SocketServer module:
import SocketServer
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print "{} wrote:".format(self.client_address[0])
print self.data
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
I can access server with http://localhost:9999/ in my computer, but I cannot access with my phone(my phone is in local area network because I connect the wifi from computer.) with IP:http://192.168.123.1:9999.
I have used python -m SimpleHTTPServer 9999 to test my network, I can access my computer with my phone.
Because the code is specifying localhost host as a host. To allow any hosts to access the port, you need to specify it as '0.0.0.0' or ''.
HOST, PORT = "", 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
When you say localhost as the server's host name, the HTTP server will pick only the requests targetted at localhost or 127.0.0.1. When you access it from your mobile, you would probably be accessing it with the actual IP address of the computer, which will not be 127.0.0.1 or localhost. That is why the server is not picking up those requests.
To specify that you want to respond to all the requests targetted at this machine, irrespective of the IP address or host name used to access the server, you would use 0.0.0.0 as the HOST
HOST, PORT = "0.0.0.0", 9999
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
import socket,ssl
import time
import Config
class StartHTTPServer:
'''Web server to serve out map image and provide cgi'''
def __init__(self):
# Start HTTP Server. Port and address defined in Config.py
srvraddr = ("", Config.HTTP_PORT) # my hostname, portnumber
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
srvrobj.socket = ssl.wrap_socket(srvrobj.socket,server_side=True,
certfile="c:\users\shuen\desktop\servertryout 23022012\serverCert.crt",
keyfile="c:\users\shuen\desktop\servertryout 23022012\privateKey.key",
ssl_version=ssl.PROTOCOL_TLSv1,
do_handshake_on_connect=True)
print srvrobj.socket.cipher()
print srvrobj.socket.getsockname()
print "HTTP server running at IP Address %s port %s." % (Config.HTTP_ADDRESS, Config.HTTP_PORT)
srvrobj.serve_forever() # run as perpetual demon
srvrobj.socket.accept()
message='hello from server.<EOF>'
srvrobj.socket.send(message)
I have tried to send data with a normal socket, which works. However, the code i'm working on is using socketServer and can't be change. I cannot find any example which will send data across to the server. How can I do that?
If you really cannot change your server software, you must use a wrapper, e. g. openssl s_server.