如何使用Python2启动一个https服务器?-python-https

1、前言

最近因为需要对正向代理的https代理功能进行测试。所以需要模拟一个简单的https服务端,在完成测试后,特将测试过程记录下来,形成此文。

2、https服务器搭建

2.1、生成自签证书

一般的机器上都会安装openssl工具,如果你的机器未安装,请首先安装openssl。

# 生成key文件(生成过程中需要输入密码,记下这个密码后面有用,假设密码为1234)
openssl genrsa -des3 -out localhost.key 1024

# 使用key文件生成证书
openssl req -new -x509 -key localhost.key -days 750 -out localhost.pem

执行完如上命令,会在当前路径下生成localhost.key和localhost.pem文件,供后面的https服务器代码使用。

2.2、编写https服务器代码

文件名:hts.py

import BaseHTTPServer
import SimpleHTTPServer
import SocketServer
import ssl

class ThreadedHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
    print "start..."
    pass

httpd = ThreadedHTTPServer(('0.0.0.0', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile="localhost.key", certfile="localhost.pem", server_side=True)
httpd.serve_forever()

2.3、启动https服务器

使用命令:python hts.py,启动过程中需要输入生成key文件时的密码1234,然后回车即可。

image-20221031151027015

然后就可以通过客户端进行访问测试了。

版权声明:除特殊说明,博客文章均为phyger原创,依据CC BY-SA 4.0许可证进行授权,转载请附上出处链接及本声明。来自:https://u1s1.vip/archives/240
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇