一行命令使用 python3 搭 https 服务
很多时候 python 搭个私有 http 服务会很方便,很常规的应用,比如传个文件啥的。有时候某些场景下要求必需是 https 服务。怎么搭呢。
原理部分
正常的搭建 https 服务过程如下:
1、 安装 openssl
2、生成证书密钥对
# 直接默认生成
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
# openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem
3、启动服务
import http.server, ssl
server_address = ('0.0.0.0', 4443)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
server_side=True,
certfile='cert.pem',
keyfile="key.pem",
ssl_version=ssl.PROTOCOL_TLSv1)
httpd.serve_forever()
但是这样好麻烦哦,我想一行命令启动
一行命令启动(低版本)
尝试使用一行命令开启
echo "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURhekNDQWxPZ0F3SUJBZ0lVV3BmZXhnRGFtYUlIY3krMzZWMDI0UTdoTzJVd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1JURUxNQWtHQTFVRUJoTUNRVlV4RXpBUkJnTlZCQWdNQ2xOdmJXVXRVM1JoZEdVeElUQWZCZ05WQkFvTQpHRWx1ZEdWeWJtVjBJRmRwWkdkcGRITWdVSFI1SUV4MFpEQWVGdzB5TXpBME1qUXdPVFF3TkRaYUZ3MHpNekEwCk1qRXdPVFF3TkRaYU1FVXhDekFKQmdOVkJBWVRBa0ZWTVJNd0VRWURWUVFJREFwVGIyMWxMVk4wWVhSbE1TRXcKSHdZRFZRUUtEQmhKYm5SbGNtNWxkQ0JYYVdSbmFYUnpJRkIwZVNCTWRHUXdnZ0VpTUEwR0NTcUdTSWIzRFFFQgpBUVVBQTRJQkR3QXdnZ0VLQW9JQkFRQzIwaTcvSy8yekhwaCtmbkIweU5kanVDaGEwd3l5cVR4RlU3NGFyamNGClZJMXh0ZU9KZkthQjU0OTJLVC9lc2gvek5SQUVHb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UV1Y0wrYVFWdldwCkhSb0llWG1zSHhQVm1yNWxvY3Zsd2pJaWdqQVN1cVlOUGxiMU1HRHBKNlNsT0tST0d2K1NLYms5T2ZOU2pqYUMKdElvRTdWN0Q2TW5lb3JxV3pmb0ZjNHI1a1E9PQotLS0tLUVORCBQUklWQVRFIEtFWS0tLS0tCg==" | base64 -d > key.pem && echo "aW1wb3J0IGh0dHAuc2VydmVyLCBzc2wKCnNlcnZlcl9hZGRyZXNzID0gKCcwLjAuMC4wJywgNDQ0MykKaHR0cGQgPSBodHRwLnNlcnZlci5IVFRQU2VydmVyKHNlcnZlcl9hZGRyZXNzLCBodHRwLnNlcnZlci5TaW1wbGVIVFRQUmVxdWVzdEhhbmRsZXIpCmh0dHBkLnNvY2tldCA9IHNzbC53cmFwX3NvY2tldChodHRwZC5zb2NrZXQsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzZXJ2ZXJfc2lkZT1UcnVlLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY2VydGZpbGU9J2NlcnQucGVtJywKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGtleWZpbGU9ImtleS5wZW0iLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc3NsX3ZlcnNpb249c3NsLlBST1RPQ09MX1RMU3YxKQpodHRwZC5zZXJ2ZV9mb3JldmVyKCk=" | base64 -d > ssss.py && python ssss.py
结果测试的时候发现,这种有的时候可以有的时候不行,提示:
/home/parallels/ssss.py:5: DeprecationWarning: ssl.wrap_socket() is deprecated, use SSLContext.wrap_socket()
这个时候访问的话,会提示使用的协议浏览器不支持
高版本
那就使用 SSLContext.wrap_socket() 开启一个试试, 其中 执行的 python 代码如下:
import http.server
import ssl
from http.server import HTTPServer, SimpleHTTPRequestHandler
class MyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
try:
# 用 SimpleHTTPRequestHandler 处理请求
return SimpleHTTPRequestHandler.do_GET(self)
except FileNotFoundError:
# 如果请求的文件不存在,返回 404 错误
self.send_error(404, 'File Not Found: %s' % self.path)
httpd = HTTPServer(('0.0.0.0', 443), MyHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
keyfile='key.pem',
certfile='cert.pem',
server_side=True)
httpd.serve_forever()
高版本一句话(直接复制,然后命令行执行即可):
echo "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURhekNDQWxPZ0F3SUJBZ0lVV3BmZXhnRGFtYUlIY3krMzZWMDI0UTdoTzJVd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1JURUxNQWtHQTFVRUJoTUNRVlV4RXpBUkJnTlZCQWdNQ2xOdmJXVXRVM1JoZEdVeElUQWZCZ05WQkFvTQpHRWx1ZEdWeWJtVjBJRmRwWkdkcGRITWdVSFI1SUV4MFpEQWVGdzB5TXpBME1qUXdPVFF3TkRaYUZ3MHpNekEwCk1qRXdPVFF3TkRaYU1FVXhDekFKQmdOVkJBWVRBa0ZWTVJNd0VRWURWUVFJREFwVGIyMWxMVk4wWVhSbE1TRXcKSHdZRFZRUUtEQmhKYm5SbGNtNWxkQ0JYYVdSbmFYUnpJRkIwZVNCTWRHUXdnZ0VpTUEwR0NTcUdTSWIzRFFFQgpBUVVBQTRJQkR3QXdnZ0VLQW9JQkFRQzIwaTcvSy8yekhwaCtmbkIweU5kanVDaGEwd3l5cVR4RlU3NGFyamNGClZJMXh0ZU9KZkthQjU0OTJLVC9lc2gvek5SQUVHbU1BcE5aMXAwZVQwN3BTak9wMHpjaWxoZFgwYUxWbkRIVW4KbmEwYzVVVHFiblM3LzhiVldKRHV5QUtMa0Y0bDUyQlVqZ3lXeDJVY1VSS1dONEFRZlNFUDdHR1hNUnk3bTEySgpBNm81dHZxaytqZzhVTXgvZVZwMlY5NW1NWHRhYU56TUVyay9XdXhrOWk5UFlLOXlnUTVqVVhhRjlibEpuVXFrCmdTRTlLNUM3SFVXUllKcGcvMTZPUU5QYjFoVU5TT1llN2tNWUtscjlvYytFUEtGaWxrT21janhvbmlH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RUUFJlcXVlc3RIYW5kbGVyCgpjbGFzcyBNeUhhbmRsZXIoU2ltcGxlSFRUUFJlcXVlc3RIYW5kbGVyKToKICBkZWYgZG9fR0VUKHNlbGYpOgogICAgdHJ5OgogICAgICAjIOeUqCBTaW1wbGVIVFRQUmVxdWVzdEhhbmRsZXIg5aSE55CG6K+35rGCCiAgICAgIHJldHVybiBTaW1wbGVIVFRQUmVxdWVzdEhhbmRsZXIuZG9fR0VUKHNlbGYpCiAgICBleGNlcHQgRmlsZU5vdEZvdW5kRXJyb3I6CiAgICAgICMg5aaC5p6c6K+35rGC55qE5paH5Lu25LiN5a2Y5Zyo77yM6L+U5ZueIDQwNCDplJnor68KICAgICAgc2VsZi5zZW5kX2Vycm9yKDQwNCwgJ0ZpbGUgTm90IEZvdW5kOiAlcycgJSBzZWxmLnBhdGgpCgpodHRwZCA9IEhUVFBTZXJ2ZXIoKCcwLjAuMC4wJywgNDQzKSwgTXlIYW5kbGVyKQpodHRwZC5zb2NrZXQgPSBzc2wud3JhcF9zb2NrZXQoaHR0cGQuc29ja2V0LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAga2V5ZmlsZT0na2V5LnBlbScsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjZXJ0ZmlsZT0nY2VydC5wZW0nLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc2VydmVyX3NpZGU9VHJ1ZSkKaHR0cGQuc2VydmVfZm9yZXZlcigp" | base64 -d > ssss.py && python ssss.py
直接复制回车就行
可惜中文会乱码,如果你的 python 是显示使用 python3 ,那么命令最后哪里改成 python3 就行
本文来自博客园,作者:Nestar,转载请注明原文链接:https://www.cnblogs.com/Nestar/p/17350782.html