티스토리 뷰
Programming/Python
[Python 2.7] 파이썬 웹 서버 오픈소스 소프트웨어 (1.0 / 20191019)
Talking1258 2019. 10. 19. 12:38파이썬을 이용해 웹 서버를 실행할 수 있다. 로우 데이터의 이해를 도울뿐더러 웹 작동원리를 알 수 있다.
목적: 다른 소프트웨어를 의존하지 않고 직접 웹 서버를 만들고, 수정하고, 가동해 보자.
제작년월: 2019년 9월 ~
최초 공개일: 2019년 10월 19일
이번 공개일: 2019년 10월 19일
버전: 1.0
소프트웨어: Python 2.7.16
필요 모듈: (기본 설치 라이브러리)
import socket
import sys
import os.path
# DEFAULT SETTING
loc = {}
loc['default'] = "/home"
loc['nofile'] = "/notfound"
loc['imagetype'] = ['ico', 'png', 'jpg']
# END OF DEFAULT SETTING
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("0.0.0.0", 80))
sock.listen(1)
def arrange(data):
header = """
HTTP/1.1 200 OK
Date: SECURITY ALERT
Server: SECURITY ALERT (MCVKR Linux)
Last-Modified: SECURITY ALERT
Content-Type: text/html; charset=UTF-8
Content-Length: """ + str(sys.getsizeof(data) - 33) + """
Accept-Ranges: bytes
Connection: close
"""
return header + data
def arrangeimage(data):
header = """
HTTP/1.1 200 OK
Date: SECURITY ALERT
Server: SECURITY ALERT (MCVKR Linux)
Last-Modified: SECURITY ALERT
ETag: "6da00e7-57e-4c8fbca4971c0"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Type: image/x-icon
Content-Length: """ + str(sys.getsizeof(data) - 33) + """
Connection: close
Content-Encoding: identity
Age: 0
"""
return header + data
archive = {}
while True:
try:
close = 0
client, ipaddr = sock.accept()
cldt = client.recv(4096)
archive['get'] = cldt.split("\n")[0].split(" ")
## SECURITY CHECK ##
print(ipaddr[0] + " Connected to " + archive['get'][1])
if archive['get'][1] and len(archive['get'][1]) > 2:
if '..' in archive['get'][1][1:]:
client.send(arrange("SECURITY ALERT"))
## END OF SECURITY CHECK LINE ##
if archive['get'][1][-1] == '/':
archive['get'][1] = archive['get'][1][:-1]
temp = archive['get'][1].split("?")
if len(temp) > 1:
getmsg = []
for i in temp[1:]:
getmsg.append(i)
archive['get'][1] = temp[0]
if archive['get'][1] == "":
archive['get'][1] = loc['default']
## PROCESS
if archive['get'][1].split('/')[-1].split('.')[-1] in loc['imagetype'] and os.path.isfile(archive['get'][1][1:]):
close = 1
f = open(archive['get'][1][1:], 'rb')
client.send(arrangeimage(f.read()))
f.close()
if close == 0 and os.path.isfile(archive['get'][1][1:]):
close = 1
f = open(archive['get'][1][1:], 'rb')
client.send(arrange(f.read()))
f.close()
elif close == 0 and len(loc['nofile']) > 1 and os.path.isfile(loc['nofile'][1:]):
close = 1
f = open(loc['nofile'][1:], 'rb')
client.send(arrange(f.read()))
f.close()
client.close()
except:
None
sock.close()
아직 보안에 취약하고, 텍스트 다운로드나 이미지 다운로드 이외에는 작동하지 않습니다.
Copyright (c) 2016-2019 MCVKR
MIT License
'Programming > Python' 카테고리의 다른 글
[Python3] 특정 색상의 선을 따라가기 위한 각도 구하기 (0) | 2019.10.23 |
---|---|
[Python 2.7] 파이썬 웹 서버 오픈소스 소프트웨어 (1.1.0 / 20191022) (0) | 2019.10.22 |
[Python3] 자리 바꾸기 프로그램 (0) | 2019.10.08 |
[Python] 멀티 프로세싱 - 병렬 프로그래밍(다중 프로세서 사용) (0) | 2019.09.22 |
[Python 2.7] 웹사이트 소스코드 가져오기 #파싱 #크롤링 (0) | 2019.09.18 |
댓글