본문 바로가기
Study/Python

Python을 통한 Simple HTTPS Server 구축

by Becoming a Hacker 2022. 8. 22.
반응형

HTTPS Server를 구축하기 위해서는 인증서가 필요한데, 이 인증서는 openssl를 통하여 생성 가능합니다.

$ openssl genrsa -out CA.key 2048
$ openssl req -x509 -new -nodes -key CA.key -days 3650 -out CA.pem
$ openssl genrsa -out server.key 2048
$ openssl req -new -key server.key -out server.csr
$ openssl x509 -req -in server.csr -CA CA.pem -CAkey CA.key -CAcreateserial -out server.crt -days 3650

 

Python Simple HTTPS Server Code

import http.server, ssl

# if change 'localhost -> 0.0.0.0', Available on the Internet
httpd = http.server.HTTPServer(('localhost', 443),  http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
                               server_side=True,
                               keyfile='server.key',
                               certfile='server.crt',
                               ssl_version=ssl.PROTOCOL_TLS)

httpd.serve_forever()

반응형

댓글