python實現靜態(tài)服務器
更新時間:2019年09月05日 08:44:32 作者:萌新小柴雞
這篇文章主要為大家詳細介紹了python實現靜態(tài)服務器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python靜態(tài)服務器的具體代碼,供大家參考,具體內容如下
#coding:utf-8
import socket
import multiprocessing
import re
HTML_ROOT_DIR = "./html"
class HTTPServer(object):
#初始化
def __init__(self):
self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#開啟服務器啟動進程
def start(self):
self.serversocket.listen(128)
while True:
clintsocket, addree = self.serversocket.accept()
hanlderprogess = multiprocessing.Process(target=self.xiexi, args=(clintsocket,))
hanlderprogess.start()
clintsocket.close()
#綁定端口
def bind(self,port):
self.serversocket.bind(("", port))
#獲取請求頭,進行解析,同時發(fā)送相應頭
def xiexi(clserver):
date = clserver.recv(1024)
print date
list = str(date).split("\r\n")
# 解析
requesthead = list[0]
print requesthead
#使用正則表達式解析出請求頭的文件名稱
file = re.match(r"\w+ +(/[^ ]*) ",requesthead).group(1)
#對獲取的問題名判斷,如果是/的就進行顯示index.html
if "/"==file:
file = "/index.html"
try:
f = open(HTML_ROOT_DIR+file,"rb")
except:
response_start_line = "HTTP/1.1 200 OK \r\n"
response_headers = "Server:My server\r\n"
response_body = "the file not file"
else:
file_date = f.readline()
f.close()
response_start_line = "HTTP/1.1 200 OK \r\n"
response_headers = "Server:My server\r\n"
response_body = file_date.decode("utf-8")
response = response_start_line+response_headers+"\r\n"+response_body
print response
clserver.send(bytes(response))
clserver.close()
def main():
server = HTTPServer()
server.bind(1001)
server.start()
if __name__=="__main__":
main()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
YOLOv5車牌識別實戰(zhàn)教程(六)性能優(yōu)化與部署
這篇文章主要介紹了YOLOv5車牌識別實戰(zhàn)教程(六)性能優(yōu)化與部署,在這個教程中,我們將一步步教你如何使用YOLOv5進行車牌識別,幫助你快速掌握YOLOv5車牌識別技能,需要的朋友可以參考下2023-04-04
Python使用pip安裝報錯:is not a supported wheel on this platform的解決
這篇文章主要介紹了Python使用pip安裝報錯:is not a supported wheel on this platform的解決方法,結合實例形式分析了在安裝版本正確的情況下pip安裝報錯的原因與相應的解決方法,需要的朋友可以參考下2018-01-01

