這篇文章主要介紹了關於python 編寫簡單網頁伺服器,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
IDE:Pycharm
#sever.py
#!/bin/python #-*- coding: UTF-8 -*- #文件名:server.py #create by wzh 2017/10/26 import socket #导入socket模块 import re from multiprocessing import Process #导入进程模块 #设置静态文件根目录 HTML_ROOT_DIR='./html' def handle_client(client_socket): """处理客户端连接请求""" request_data=client_socket.recv(1024) print(request_data) request_lines=request_data.splitlines() for line in request_lines: print(line) #'GET / HTTP/1.1' request_start_line=request_lines[0].decode("utf-8") print("*"*10) print(request_start_line) #提取用户请求的文件名 file_name=re.match(r"\w+ +(/[^ ]*) ",str(request_start_line)).group(1) if "/" == file_name: file_name='/index.html' #打开文件,读取内容 try: file=open(HTML_ROOT_DIR+file_name,"rb") except IOError: response_start_line="HTTP/1.1 404 Not Found\r\n" response_heads="Server: My server\r\n" response_body="The file not found!" else: file_data=file.read() file.close() response_start_line="HTTP/1.1 200 ok\r\n" response_heads="Server: My server\r\n" response_body=file_data.decode("utf-8") response=response_start_line+response_heads+"\r\n"+response_body print("response data:",response) client_socket.send(bytes(response,"utf-8")) client_socket.close() if __name__=="__main__": #如果直接运行本文件,那么__name__为__main__(此时才运行下面的程序),否则为对应包名 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 创建socket对象 s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) #host = socket.gethostname() # 获取本地主机名 port = 1212 # #print(host) s.bind(("", port)) # 绑定端口 s.listen(5) while True: c,addr=s.accept() #建立客户端连接 print('连接地址',addr) handle_client_process=Process(target=handle_client,args=(c,)) #ALT+ENTER快捷键生成函数 handle_client_process.start() c.close()
#index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My Web</title> </head> <h1 id="welcome">welcome!</h1> <p align="center">这是一个神奇的网站!</p> <body> </body> </html>
執行server.py
在瀏覽器中輸入localhost:1212
相關推薦:
##################################################### #################
以上是python 編寫簡單網頁伺服器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中