How do I launch Python SimpleHTTPServer on heroku? - python

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

Related

error when trying to connect to my websitename(Centos)

I'm trying to set up very basic http server on my hosting(centos)
The script is:
#!/usr/bin/env python3.8
from http.server import HTTPServer, BaseHTTPRequestHandler
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'Hello, world!')
httpd = HTTPServer(('0.0.0.0', 8080), SimpleHTTPRequestHandler)
httpd.serve_forever()
Everything looks normal: ss -t -a -n
LISTEN 0 5 0.0.0.0:8080 0.0.0.0:*
But when im trying to get to my websitename:8080 nothing is happening and i get connection timed out response.
I've been googling about http, networking, even centos for a few days, got lots of usefull information but i still have no idea what's going on here.
Ty for your time!

docker python simplehhtpserver not working

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

How to get client IP from SimpleHTTPServer

Building a simple file server using the SimpleHTTPServer module in Python, however I'm running into issues when trying to get the IP from a connecting client. Here is what I have..
import SimpleHTTPServer
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", 8080), Handler)
print "Serving local directory"
while True:
httpd.handle_request()
print Handler.client_address[0]
When a client connects I get..
AttributeError: class SimpleHTTPRequestHandler has no attribute 'client_address'
I know this is because I haven't instantiated the class yet, but is there another way to get the IP from the client without having to create a handler instance? The client's IP is outputted to the console when a connection is made, I just need a way to grab that IP within my script.
Thanks!
Indeed, the Handler class object is unrelated to specific instances. Set up your own handler class, like this:
import SimpleHTTPServer
import SocketServer
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def handle_one_request(self):
print(self.client_address[0])
return SimpleHTTPServer.SimpleHTTPRequestHandler.handle_one_request(self)
print("Serving local directory")
httpd = SocketServer.TCPServer(("", 8080), MyHandler)
while True:
httpd.handle_request()

Natting of AWS EC2 public IP and primary private ip

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!

python ThreadedTCPServer can only access local connections on windows 7

I'm using ThreadedTCPServer to start a TCP server. Here is the code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import threading
import SocketServer
import time
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
recv1 = self.request.recv(1)
print "server: %s" % recv1
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
if __name__ == "__main__":
server = ThreadedTCPServer(('0.0.0.0', 8080), ThreadedTCPRequestHandler)
print server.server_address
# Start a thread with the server -- that thread will then start one
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
server_thread.daemon = True
server_thread.start()
print "Server loop running in thread:", server_thread.name
time.sleep(30)
server.shutdown()
print "end"
I'm working on Windows7(IP: 192.168.1.180)/Python2.7, when starting the program, I can telnet the server from local, but from another machine, I can't telnet success.
> telnet 192.168.1.180 8080
But, I run the program on Linux, it works fine. I can telnet it successfully from another machine.
Why on Windows7 it can not receive remote connection? I checked the net status on windows7 during running:
C:\Users\Henry>netstat -ant | findstr 8080
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING InHost
Sorry, problem solved. It IS a environment problem. It's blocked by the windows7 firewall.
Your program does seem to be listening for connections correctly. And looking at this post from the superuser's stack exchange, it appears that your configuration is correct. See:
https://superuser.com/questions/386436/the-meaning-of-port-0-in-netstat-output
If your script works in one place but not another, I would look at the environmental differences. Since your client can't connect to the server, I would guess you got some kind of a connection refused error.
Is your windows firewall (or some other third party firewall) blocking inbound connections on port 8080? That seems likely since it is a port commonly used by web servers.

Categories

Resources