This question already has answers here:
Deploying a minimal flask app in docker - server connection issues
(8 answers)
Closed 10 months ago.
I am unable to access the app outside the container, when I exec inside the container and do curl localhost:8000 it is showing but the same app is not reflecting in the host browser. Any help would be appreciated.
main.py
#!/usr/bin/env python
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse
import json
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
parsed_path = urlparse(self.path)
self.send_response(200)
self.end_headers()
self.wfile.write(json.dumps({
'myfavourite author name': 'shakespear',
}).encode())
returnx
if __name__ == '__main__':
server = HTTPServer(('localhost', 8000), RequestHandler)
print('Starting server at http://localhost:8000')
server.serve_forever()
Dockerfile
FROM python:latest
COPY . ./
RUN pip install -r requirements.txt
EXPOSE 8000
CMD python main.py
requirements.txt
urllib3
httpserver
You need to bind to 0.0.0.0 for your program to accept connections from outside the container. Like this
if __name__ == '__main__':
server = HTTPServer(('0.0.0.0', 8000), RequestHandler)
print('Starting server at http://0.0.0.0:8000')
server.serve_forever()
Related
I am using python http.server 80 to expose my downloaded files to my twilio whatsapp bot
is there a way that as my django-twillio app starts it automatically runs the server on port 80 as well
python -m http.server 80
Adding this code to your django-twilio app will programmatically start your server on localhost:80
from http.server import HTTPServer, SimpleHTTPRequestHandler
httpd = HTTPServer(('localhost', 80), SimpleHTTPRequestHandler)
httpd.serve_forever()
How to run a server in python?
I already have tried:
python -m SimpleHTTPServer
python -m HTTPServer
but its says to me:
invalid syntax
Can someone help me?
Thanks!
You can use this command in cmd or terminal
python -m SimpleHTTPServer <port_number> # Python 2.x
Python 3.x
python3 -m http.server # Python 3x
By default, this will run the contents of the directory on a local web server, on port 8000. You can go to this server by going to the URL localhost:8000 in your web browser.
I have made a remote access program that uses the Socket module. If you want to copy the code, that's fine. EDIT: You will need to run it using a cmd file like this: "python (filename).py." After that, you will need to add the line "pause"
#SERVER
import os
import socket
s = socket.socket()
host = socket.gethostname()
port = 8080
s.bind((host, port))
print("Server started at: ", host)
s.listen(1)
conn,addr = s.accept()
print(addr, "connected")
#CLIENT
import os
import socket
s = socket.socket()
port = 8080
host = "YOUR DESKTOP ID" (Your server should say it. I.E. "Server started at: (Desktop-123456)")
I'm running a FastAPI app in Python using uvicorn on a Windows machine. It works fine when I do any one of the following options:
Run the following code on my mac, or
When I don't specify the port for uvicorn (remove the host parameter from the uvicorn.run call)
When I specify port '127.0.0.1', which is the host it uses when I don't specify a host at all.
from fastapi import FastAPI
import uvicorn
app = FastAPI()
#app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == '__main__':
uvicorn.run(app, port=8080, host='0.0.0.0')
When I go to 0.0.0.0:8080 on my browser, I get an error that says "This site can’t be reached".
I have checked my current active ports to make sure I'm not getting a collision using netstat -ao |find /i "listening" and 0.0.0.0:8080 is not in use.
My current file configuration looks like this:
working_directory
└── app
├── gunicorn_conf.py
└── main.py
My gunicorn_conf.py is super simple and just tries to set the host and port:
host = "0.0.0.0"
port = "8080"
How can I get this to work when I specify host '0.0.0.0'?
As I was writing the question above, I found the solution and thought I would share in case someone else runs into this. To get it to work put "http://localhost:8080" into the web browser instead of "http://0.0.0.0:8080" and it will work fine. This also works if you're hitting the endpoint via the python requests package, etc.
Run this in terminal uvicorn main:app --port 8086 --reload
I have written a simple Python Flask application as follows:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello_world():
return 'Hello World2'
if __name__ == '__main__':
app.run(debug=True, port=5000)
This code is then executed in my Virtual box Ubuntu 18.04 Server VM. It starts listening to port 5000 in my VM.
However, when I try to access it from my host browser at 127.0.0.1:6000, it is not loading.
I have enabled port forwarding in Virtualbox NAT port forwarding option as shown below:
How to access the Flask server from host?
Most probably your application binds to loopback network interface.
Change it to bind to all interfaces so it is accessible from the outside:
app.run(host='0.0.0.0', debug=True, port=5000)
This question already has answers here:
Deploying a minimal flask app in docker - server connection issues
(8 answers)
Closed 5 years ago.
I've got a flask application with SSL authorization.
Here is my run.py:
#!flask/bin/python
from app import app
import ssl
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ctx.load_cert_chain('sertnew/se-emulator.crt', 'sertnew/se-emulator.key')
app.run(debug=True, host='127.0.0.1', port=5000, ssl_context=ctx)
On my machine, I run it simply with
python run.py
Then I open https://localhost:5000 in chrome and it works (there is a message of non-secure connection, but it's ok for me)
Now I'm trying to make it work in Docker container.
I've got a Dockerfile like this:
FROM python:3.5-slim
RUN apt-get update && apt-get install -y python3-pip
COPY . /storage-emulator
WORKDIR /storage-emulator
RUN pip3 install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["run.py"]
and try to run it in different ways.
I can see "Running on https://127.0.0.1:5000/ (Press CTRL+C to quit)" message, but can't open the page in the browser. What am I doing wrong?
This is a rather easy fix, you have to change this line:
app.run(debug=True, host='127.0.0.1', port=5000, ssl_context=ctx)
to
app.run(debug=True, host='0.0.0.0', port=5000, ssl_context=ctx)
You have to think from the containers' perspective: The container has its own "localhost", which is different from the localhost of the host machine, all of that means that flask has never received the request.
Therefore you can simply bind to all IPs within the container, which is done by binding to "0.0.0.0".