Junior — Middle
How can HTTPS be configured for a web service or website?
sobes.tech AI
Answer from AI
To set up HTTPS for a web service or website in Python, you typically use a web server or framework that supports SSL/TLS. The main steps:
- Obtain an SSL certificate (e.g., from Let's Encrypt or a commercial CA).
- Configure your web server (e.g., Nginx, Apache) to serve HTTPS, specifying the paths to your certificate and private key.
- If you're using a built-in Python server (like Flask or Django for development), you can use the
ssllibrary to wrap the socket.
Example for Flask with SSL:
from flask import Flask
import ssl
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, HTTPS!"
if __name__ == '__main__':
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.load_cert_chain('path/to/cert.pem', 'path/to/key.pem')
app.run(host='0.0.0.0', port=443, ssl_context=context)
However, for production, it is recommended to use a full-featured web server (like Nginx, Apache) or cloud solutions that manage certificates and security reliably.