How to forward http to https in cherrypy native web server - cherrypy

I've a Cherrypy website that is running well on https, I can run the same server on http port without forward like this:
from cherrypy._cpserver import Server
server2 = Server()
server2.socket_host = "123.123.123.123"
server2.socket_port = 80
server2.subscribe()
I can run another Cherrypy instance on http port and forward it https by having rise cherrypy.HTTPRedirect in the class:
class HelloWorld(object):
#cherrypy.expose
def index(self):
raise cherrypy.HTTPRedirect("https://example.com", status=301)
Is there a way to forward http to https without running another server or using 3rd party service?

Related

Run Flask without port

I have a flask server running on ubuntu. I want to hit the server using my domain name, test.example.com, without having to include the port number. Right now, I can successfully access the server by doing https://test.example.com:80/ but I can't figure out how to do just https://test.example.com/
In flask_server.py:
if __name__ == '__main__':
app.run(host=0.0.0.0, port=80, ssl_conext=context)
Use a proxy forwading, like a basic NGinx server in front of your flask website
Nginx gonna take the HTTPS/Domain on the 443 port part and forward the query to you local flask server on the localhost:80

Running Flask application without nginx

Running Flask application without nginx or other web server?
Can uWSGI be a web-server and application server at the same time?
For example, stand-alone WSGI Containers
https://flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone/
But again, it recommends to use an HTTP server. Why? Can't uWSGI handle HTTP requests?
I have read different articles about deploying a Flask application. They say, I'd need uWSGI and nginx - one popular option.
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04
https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
https://flask.palletsprojects.com/en/1.1.x/deploying/uwsgi/#uwsgi
My Flask application. app_service.py
import json
import os
from flask import Flask, Response, redirect
portToUse = 9401
#app.route("/app/people")
def get_service_people():
print("Get people")
people_str = "{ \"John\", \"Alex\" }"
return Response(people_str, mimetype="application/json;charset=UTF-8")
if __name__ == "__main__":
app.run(host='0.0.0.0', port=portToUse)
uwsgi config uwsgi.ini
[uwsgi]
chdir = $(APPDIR)
wsgi-file = app_service.py
callable = app
uid = psc-user
gid = psc-user
master = true
processes = 1
threads = 1
http-timeout = 300
socket-timeout = 300
harakiri = 300
http = 0.0.0.0:9401
socket = /tmp/uwsgi.socket
chmod-sock = 664
vacuum = true
die-on-term = true
; Images serving: https://github.com/unbit/uwsgi/issues/1126#issuecomment-166687767
wsgi-disable-file-wrapper = true
log-date = %%Y-%%m-%%d %%H:%%M:%%S
logformat-strftime = true
logformat = %(ftime) | uWSGI | %(addr) (%(proto) %(status)) | %(method) %(uri) | %(pid):%(wid) | Returned %(size) bytes in %(msecs) ms to %(uagent)
requirements.txt
# Web framework for python app.
Flask==1.1.1
# JWT tocket utils to retrieve the tocken from HTTP request header.
# It is used for retrieving optional permissions from gateway.
# https://pypi.org/project/PyJWT/
PyJWT==1.7.1
# Eureka API client library to implement service discovery pattern
py_eureka_client==0.7.4
# Python application server
uWSGI==2.0.18
And it seems to be working. I am running all this in a virtual machine in docker-compose.
My question, why do I need nginx here? Do python developers use uWSGI without a web server?
Update
I am not going to run dev default WSGI server in production, as it is asked here
Are a WSGI server and HTTP server required to serve a Flask app?
WSGI servers happen to have HTTP servers but they will not be as good as a dedicated production HTTP server (Nginx, Apache, etc.)
from
https://stackoverflow.com/a/38982989/1839360
Why is it so?
What I am asking is why uWSGI server cannot be as good for HTTP handling, so I need to put HTTP server between the internet and uWSGI there. Why incoming HTTP requests can go directly to uWSGI (it is not running in the development or debug mode).
For running flask, you do not need nginx, just a webserver of your choice, but life with nginx is just easier. If you are using Apache, you want to consider to use a WSGI.
I remember reading somewhere in the Flask documentation what is stated by an answer to "Are a WSGI server and HTTP server required to serve a Flask app?" as
The answer is similar for "should I use a web server". WSGI servers happen to have HTTP servers but they will not be as good as a dedicated production HTTP server (Nginx, Apache, etc.).
The main idea behind is the architectural principle of splitting layers to ease debugging and increase security, similarly to the concept that you split content and structure (HTML & CSS, UI vs. API):
For the lower layers, see e.g. https://en.wikipedia.org/wiki/Transport_layer
Having a dedicated HTTP server allows you to do package-filtering etc. on that level.
The WSGI is the interface layer between the webserver and the webframework.
Update
I have seen clients only running a WSGI server alone, with integrated HTTP support. Using an additional webserver and/ or proxy is just good practice, but IMHO not strictly necessary.
References
https://flask.palletsprojects.com/en/1.1.x/deploying/mod_wsgi/ describes the Apache way for flask
https://flask.palletsprojects.com/en/1.1.x/tutorial/deploy/ elaborates on how a production environment should look like
Deploying Python web app (Flask) in Windows Server (IIS) using FastCGI
Debugging a Flask app running in Gunicorn
Flask at first run: Do not use the development server in a production environment

Why are HTTP curl/Postman requests to a HTTPS server working? (Node, AWS)

I've set up a HTTPS Node server on AWS EC2 which runs on port 4443 and set up Nginx to redirect requests(port 443).
https.createServer(options, app).listen(4443, function(){
console.log("https server started at port 4443");
});
The HTTPS server is working fine, I get a secure connection on browsers and
curl https://mydomain/apiroute works
Now, when I do
curl http://mydomain/apiroute the requests still work. The same goes for Postman.
I know that I can redirect all HTTP traffic to HTTPS using something like 301 redirects but my question is, why does the server respond to HTTP API calls inspite of there being no "HTTP" server?
Additionally, I use a letsencrypt generated certificate.

Redirect http request(via browser) to local server

I have nodejs http server running on port 9000.
Now for some hostname how do i redirect normal request to this port.
Example : if I hit www.specficDomain.com in browser, it should go to www.specficDomain.com:9000.
Thanks,
Shantanu

Running a forward proxy server in nodejitsu using node js

I am new to proxy server. What I want to do is: I want to write some node.js code, and then upload to my nodejitsu account to run as a proxy server. Then I would like to use my nodejitsu proxy server on my computer, by configuring the http proxy as "abc.jit.su" (my jitsu URL), and the port as "80" in Chrome, Firefox or IE. That's to say, I want my nodejitsu proxy server to have the same function as the proxies listed here: http://www.freeproxylists.net/. Any ideas?
You can write a simple proxy using the request module, like this:
var http = require('http'),
request = require('request');
// For nodejitsu, this will be port 80 externally
var port = process.env.PORT || 8000;
http.createServer(function(req,res) {
req.pipe(request(req.url)).pipe(res)
}).listen(port);
However, this will only work with http, not https.
Nodejitsu also produces a proxy module, you may get some ideas on what to do next by looking at that.

Resources