I've got two python servers locally hosting both the dynamic and static pages of a site.
I'm showing the dynamic page in an iframe on a static page, and want to send data to the parent static page using the javascript parent.functionx(data) approach, but I get error:
Uncaught SecurityError: Blocked a frame with origin "http://localhost:8001" from accessing a frame with origin "http://localhost:8000". Protocols, domains, and ports must match.
Can I host these both on the same port, and ideally run a single script to launch both static and dynamic pages?
A WSGI server on port 8001 to host a dynamic python page, accessed by the path /meas
cgiserver.py
import subprocess
from cherrypy import wsgiserver
def application(env, start_response):
if '/meas' in env['PATH_INFO']:
start_response('200 OK', [('Content-Type', 'text/html')])
pathargs = env['PATH_INFO']
args = pathargs.split('/')
participantid = args[2]
studyid = args[3]
repeatid = args[4]
proc = subprocess.Popen(['python3', 'cgi-bin/script-meas.py', participantid, studyid, repeatid], stdout=subprocess.PIPE)
line = proc.stdout.readline()
while line:
yield line
line = proc.stdout.readline()
wsgi_server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8001), application)
wsgi_server.start()
A CGI server on port 8000 to host static files (of which there are many and complex folder structures)
wsgiserver.py
from http.server import CGIHTTPRequestHandler, HTTPServer
handler = CGIHTTPRequestHandler
handler.cgi_directories = ['/cgi-bin', '/htbin'] # this is the default
server = HTTPServer(('localhost', 8000), handler)
print('Serving on localhost:8000');
server.serve_forever()
Thank you for clarifying your needs.
In your use-case to have multiple processes using the same IP/port, you should put them behind a third process (apache/nginx/lighttpd/...).
Considering your question, I suppose you are in a developpment environment and it might be hard to setup and configure a webserver, so you could use a HTTP proxy (found SimpleHTTPProxy and ProxyHTTPServer and the related post "seriously simple python HTTP proxy?", but check the web for more); overall, this solution would not be easier...
The general idea is to configure the third program to listen to a port (ie: 80) and to serve the request to either of the backends depending of the requested path; ie:
When the process (say apache) listening to a port (let's say port 80) get a request to /meas it will redirect it to the wsgi server (either on a Unix socket or on the port 8081), if it is a request to /cgi-bin or /htbin it will redirect it to the CGI handler (actually Apache have a cgi-bin module and you could remove this backend).
Related
I would like to deploy a Python app than can start an internal webserver on 127.0.0.1/::1 on whatever port is free at that time. It then should start a web browser with a URL pointing to the local adhoc web server.
My problem now is: are there any WSGI web servers that allow to dynamically bind them to a free port, and then make the port number choosen available to the program? Or can I create a listening socket myself and then hand it over to a WSGI server to use?
Using wsgiref.simple_server:
import wsgiref.simple_server
your_app = wsgiref.simple_server.demo_app
server = wsgiref.simple_server.make_server('127.0.0.1', 0, your_app)
_, port = server.server_address
# start the browser, perhaps on a separate thread after a delay
server.serve_forever()
Using Cheroot: there might be an easier way, but this kind of works:
import threading, time, cheroot.wsgi
server = cheroot.wsgi.Server(('127.0.0.1', 0), your_app)
def get_port():
while not server.ready:
time.sleep(0.1)
_, port = server.bind_addr
# start the browser
threading.Thread(target=get_port).start()
server.start()
I have python app on Bluemix and would like to make it accessible over https only. By default I can connect both via http and https. Want to restrict access via https only. So what is the best way to disable http access, or redirect request to https only?
As ralphearle mentioned in his answer, Bluemix proxy server terminates the SSL, so you can look into the X-Forwarded-Proto header to find out if request comes from http or https.
See below a simple example based on the Python starter code from Bluemix. I added the RedirectHandler class to check for the X-Forwarded-Proto header value and redirects the request to https if it not https.
import os
try:
from SimpleHTTPServer import SimpleHTTPRequestHandler as Handler
from SocketServer import TCPServer as Server
except ImportError:
from http.server import SimpleHTTPRequestHandler as Handler
from http.server import HTTPServer as Server
class RedirectHandler(Handler):
def do_HEAD(self):
if ('X-Forwarded-Proto' in self.headers and
self.headers['X-Forwarded-Proto'] != 'https'):
self.send_response(301)
self.send_header("Location", 'https://' + self.headers['Host'] + self.path)
self.end_headers()
def do_GET(self):
self.do_HEAD()
Handler.do_GET(self)
# Read port selected by the cloud for our application
PORT = int(os.getenv('PORT', 8000))
# Change current directory to avoid exposure of control files
os.chdir('static')
httpd = Server(("", PORT), RedirectHandler)
try:
print("Start serving at port %i" % PORT)
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
The Bluemix proxy server terminates the SSL, so that all traffic looks like HTTP to your app. However, the proxy also adds a special HTTP header named $WSSC with a value that can be either http or https. Check this header and, if the value is set to http, then change it to https.
As Adam pointed out in his comment, the IBM forum has further discussion of this approach: https://developer.ibm.com/answers/questions/16016/how-do-i-enforce-ssl-for-my-bluemix-application.html
I have a flask app that I want to deploy using CherryPy's built in server. I chose CherryPy so that the app can be deployed without having to reverse proxy (ie. nginx in front).
I'm having trouble getting CherryPy to listen for requests on just a single hostname.
Say I'm serving 2 sites: test1.com and test2.com (and have them set in my hosts file to point back to localhost).
My /etc/hosts file:
127.0.0.1 test1.com test2.com
CherryPy is serving test1.com, test2.com doesn't have anything serving it.
My cherrypy file is as follows:
import cherrypy
from my_test_flask_app import app
if __name__ == '__main__':
cherrypy.tree.graft(app, "/")
cherrypy.server.unsubscribe()
server = cherrypy._cpserver.Server()
server.socket_host = "test1.com"
server.socket_port = 8030
server.thread_pool = 30
server.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
Set up this way, I go to test1.com:8030 on my browser and it works as expected.
But when I go to test2.com:8030, the same app is served. I expected it not to serve anything, since CherryPy isn't set up to listen for test2.com.
To me, it seems that CherryPy is just listening for everything on the given port (8030), and treating the socket_host part as if its 0.0.0.0
Am I missing something here? I've looked through lots of docs and tutorials, but all things suggest that this code snippet should be working as I expected.
Thanks
Here's how you can setup what you want...
root = Root()
RootApp = cherrypy.Application(root)
Domain2App = cherrypy.Application(root)
SecureApp = cherrypy.Application(Secure())
vhost = cherrypy._cpwsgi.VirtualHost(RootApp,
domains={'www.domain2.example': Domain2App,
'www.domain2.example:443': SecureApp,
})
cherrypy.tree.graft(vhost)
https://cherrypy.readthedocs.org/en/3.3.0/refman/_cpwsgi.html#classes
Hope this helps!
You misunderstand the socket listen address - they are IP addresses only, not on DNS names. Set this way, CherryPy listens to the localhost (127.0.0.1) only - try using your Ethernet/Wlan local address and you should get connection refused.
Also, you can wrap your application with a WSGI middleware that checks the Host header for the proper domain, or use CherryPy virtual host facility to check the host header.
Is there a way to make Python SimpleHTTPServer supports mod_rewrite?
I'm trying things with Ember.js with leveraging History API as the location API, and to make it work, I have to :
1) add some vhosts config in WAMP (not simple), or
2) run python -m simpleHTTPServer (very simple)
So when I opened it in the browser, localhost:3000 and clicked around the navigation (about and users for example), it worked well. The URLs are changed by Ember.js to localhost:3000/about and localhost:3000/users respectively.
But when I tried to open localhost:3000/about directly in new tab, the python web server simply returns 404.
I had my .htaccess redirecting everything to index.html, but I suspect python simple web server doesn't really read the htaccess file (am I right on this?)
I've tried downloading PHP 5.4.12 and run the built in web server, the url and htaccess mod_rewrite works well. But I'm still reluctant to upgrade from stable 5.3 to (probably still unstable enough) 5.4.12, so if there's a way to support mod_rewrite in python simple web server, that would be preferrable.
Thanks for the answer.
By modifying pd40's answer, I came up with this which doesn't redirect, it does your traditional "send index.html instead of 404". Not at all optimized, but it works for testing and development which is all I needed.
import SimpleHTTPServer, SocketServer
import urlparse, os
PORT = 3456
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
# Parse query data to find out what was requested
parsedParams = urlparse.urlparse(self.path)
# See if the file requested exists
if os.access('.' + os.sep + parsedParams.path, os.R_OK):
# File exists, serve it up
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);
else:
# send index.hmtl
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
with open('index.html', 'r') as fin:
self.copyfile(fin, self.wfile)
Handler = MyHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
SimpleHTTPServer does not support apache modules and does not respect .htaccess, because it isn't apache. it won't work with php either.
If you know the cases you need to redirect you can subclass SimpleHTTPRequestHandler and do a redirect. This redirects any missing file requests to /index.html
import SimpleHTTPServer, SocketServer
import urlparse, os
PORT = 3000
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
# Parse query data to find out what was requested
parsedParams = urlparse.urlparse(self.path)
# See if the file requested exists
if os.access('.' + os.sep + parsedParams.path, os.R_OK):
# File exists, serve it up
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);
else:
# redirect to index.html
self.send_response(302)
self.send_header('Content-Type', 'text/html')
self.send_header('location', '/index.html')
self.end_headers()
Handler = MyHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
No mod_rewrite in Python servers I am afraid, unless you run python scripts behind an Apache server, a resource-costly solution.
Try Cherrypy (http://www.cherrypy.org/), which allows you to manage your page handlers, and very simply makes clean URLs.
I am writing an rather simple http web server in python3. The web server needs to be simple - only basic reading from config files, etc. I am using only standard libraries and for now it works rather ok.
There is only one requirement for this project, which I can't implement on my own - virtual hosts. I need to have at least two virtual hosts, defined in config files. The problem is, that I can't find a way how can I implement them in python. Does anyone have any guides, articles, maybe some simple implementation how can this be done?
I would be grateful for any help.
Virtual hosts work by obeying the Host: header in the HTTP request.
Just read the headers of the request, and take action based on the value of the Host: header
For a simple HTTP web server, you can start with the WSGI reference implementation:
wsgiref is a reference implementation of the WSGI specification that can be used to add WSGI support to a web server or framework. It provides utilities for manipulating WSGI environment variables and response headers, base classes for implementing WSGI servers, a demo HTTP server that serves WSGI applications,...
Modifying the example server to check the HTTP_HOST header, here is a simple app that responds, depending on the virtual host, with a different text. (Extending the example to use a configuration file is left as an exercise).
import wsgiref
from wsgiref.simple_server import make_server
def my_app(environ,start_response):
from io import StringIO
stdout = StringIO()
host = environ["HTTP_HOST"].split(":")[0]
if host == "127.0.0.1":
print("This is virtual host 1", file=stdout)
elif host == "localhost":
print("This is virtual host 2", file=stdout)
else:
print("Unknown virtual host", file=stdout)
print("Hello world!", file=stdout)
print(file=stdout)
start_response(b"200 OK", [(b'Content-Type',b'text/plain; charset=utf-8')])
return [stdout.getvalue().encode("utf-8")]
def test1():
httpd = make_server('', 8000, my_app)
print("Serving HTTP on port 8000...")
# Respond to requests until process is killed
httpd.serve_forever()