Blog/content/posts/2022/python/local-ssl-server.md

37 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "🌐 Локальный SSL HTTP сервер на Python"
date: 2022-11-02T00:15:40+03:00
draft: false
tags: [privacy, python, tips]
---
При необходимости запустить локальный SSL HTTP сервер,
необходимо прочитать пост дальше этой строчки.
## Генерируем сертификат
Для генерации сертификата и приватного ключа необходимо выполнить следующие команды:
```sh
openssl genrsa 2048 > key.pem
openssl req -x509 -days 365 -new -key key.pem -out cert.pem
# далее нужно заполнить необходимую информацию
```
В директории появятся два файла: `cert.pem` и `key.pem`.
## Запускаем сервер на Python
Код сервера:
```python
from http.server import HTTPServer, SimpleHTTPRequestHandler
import ssl
httpd = HTTPServer(('localhost', 1443), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='cert.pem', keyfile='key.pem', server_side=True)
httpd.serve_forever()
```
Не забываем, что сервер доступен по протоколу **https://**.