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()
Related
This is a simple HTTPS python server (not for production use)
# libraries needed:
from http.server import HTTPServer, SimpleHTTPRequestHandler
import ssl , socket
# address set
server_ip = '0.0.0.0'
server_port = 3389
# configuring HTTP -> HTTPS
httpd = HTTPServer((server_ip, server_port), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='./public_cert.pem',keyfile='./private_key.pem', server_side=True)
httpd.serve_forever()
as you see in the above script i wrap a simple python HTTP server with ssl certificate and make it a simple HTTPS server
Detailed example of python simple HTTPS server
Is there a way to ssl wrap any, already running HTTP server to HTTPS server ?
Suppose I have a HTTP server running at port 8080. Can I just ssl wrap it to port 443?
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()
I am trying to run a simple web server with Python. It works fine when I run with below command.
python -m http.server 8000
But when I make a Windows service with same code, it fails with error 1053.
sc create TestWebServer binPath="C:\Python37\python.exe -m http.server 8000" start=auto
I found that some relative posts say debug can solve this problem, but it doesn't work for me.
sc create TestWebServer binPath="C:\Python37\python.exe -m http.server 8000 debug" start=auto
Edited
I made a python code like below.
import os
from http.server import HTTPServer, CGIHTTPRequestHandler
os.chdir('.')
server = HTTPServer(server_address=('', 8000),
RequestHandlerClass=CGIHTTPRequestHandler)
server.serve_forever()
And I made it as Windows service, but it still fails with error 1053.
sc create TestWebServer binPath="C:\Python37\python.exe C:\TestWebServer.py" start=auto
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 wrote a python XMLRPC server for my web application. The problem is whenever I start the server from shell and exit, xmlrpc server stops as well. I tried executing server script from another file thinking that it will continue to run in the background but that didn't work. Here's the code used to start a server.
host = 'localhost'
port = 8000
server = SimpleXMLRPCServer.SimpleXMLRPCServer((host, port))
server.register_function(getList)
server.serve_forever()
In the shell I just do >>python MyXmlrpcServer.py to start a server.
What do I do to be able to start a server and keep it running?
#warwaruk makes a useful suggestion; Twisted XML-RPC is simple and robust. However, if you simply want to run and manage a python process in the 'background' take a look at Supervisord. It is a simple process management system.
$ pip install supervisor
$ echo_supervisord_conf > /etc/supervisord.conf
Edit that config file to add a definition of your process thus...
[program:mycoolproc]
directory=/path/to/my/script/dir
command=python MyXmlrpcServer.py
Start supervisord and start your process
$ supervisord
$ supervisorctl start mycoolproc
Better use twisted to create an XML-RPC server. Thus you will not need writing your own server, it is very flexible, and you will be able to run in background using twistd:
#!/usr/bin/env python
import time, datetime, os, sys
from twisted.web import xmlrpc, server
from twisted.internet import reactor
class Worker(xmlrpc.XMLRPC):
def xmlrpc_test(self):
print 'test called!'
port = 1235
r = Worker(allowNone=True)
if __name__ == '__main__':
print 'Listening on port', port
reactor.listenTCP(port, server.Site(r))
reactor.run()
else: # run the worker as a twistd service application: twistd -y xmlrpc_server.py --no_save
from twisted.application import service, internet
application = service.Application('xmlrpc_server')
reactor.listenTCP(port, server.Site(r))
reactor.run()
#internet.TCPServer(port, server.Site(r)).setServiceParent(application)