利用python编写设计多线程web服务器(计算机网络_自顶向下第六版_第二章1和4的编程作业)
生活随笔
收集整理的這篇文章主要介紹了
利用python编写设计多线程web服务器(计算机网络_自顶向下第六版_第二章1和4的编程作业)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
???????今天翻看自己以前的博客時,發現了這則博客,距今大約也有一年多的時間了,覺得還是蠻有趣的一個作業,于是跟著博客又做了一遍,覺得之前的排版有點不大好,所以此番用markdown 稍微重做些修改更新一下博客。
???????顧名思義,本博客是介紹如何用python編寫一個多線程web服務器,于是我利用socket模塊寫了個TCP服務器,程序很簡單,運行程序后,通過瀏覽器端輸入:
localhost:port/xxxxxx可以訪問到本地文件目錄中的文件,我在本地編寫了兩個html文件用于訪問測試:
-
Success.html
<!DOCTYPE html> <html> <head><title></title> <meta http-equiv="content-type" content="text/html"; charset="utf-8"/> </head> <body> </br> <center><h2>Connect to local web server!</h2></center> <center><img src="http://i2.sinaimg.cn/ent/m/c/2008-03-16/U2507P28T3D1949676F326DT20080316182933.jpg" width="300px"/></center> </br> <center><h4><a href="https://blog.csdn.net/wobeatit/article/details/88077445">MyCSDN</a></h4></center> </body> </html> -
Fail.html
<!DOCTYPE html> <html> <head><title></title> <meta http-equiv="content-type" content="text/html"; charset="utf-8"/> </head> <body> <center><h2>所訪問的路徑不存在!</h2></center> </br> <!--打開網頁圖片--> <center><img src="https://image.freepik.com/free-vector/404-error-web-template-with-cow-flying-flat-style_23-2147777436.jpg" width="400px"/></center> </br></br> <center><h4><a href="https://blog.csdn.net/wobeatit/article/details/88077445">LQ的博客</a></h4><center> </body> </html> -
附上Python源代碼:
具體實現的功能有:
-
向服務器請求特定文件,從服務器文件系統讀取請求的文件,并創建一個由請求的文件的html內容組成的HTTP響應報文反饋給瀏覽器并顯示出來
HTTP/1.1 200 OK<!DOCTYPE html> <html> <head><title></title> <meta http-equiv="content-type" content="text/html"; charset="utf-8"/> </head> <body> </br> <center><h2>Connect to local web server!</h2></center> <center><img src="http://i2.sinaimg.cn/ent/m/c/2008-03-16/U2507P28T3D1949676F326DT20080316182933.jpg" width="300px"/></center> </br> <center><h4><a href="https://blog.csdn.net/wobeatit/article/details/88077445">MyCSDN</a></h4></center> </body> </html> 從后臺可以看到請求的文件的內容
-
多線程
-
異常檢測
訪問服務器中不存在的文件路徑時,后臺會返回訪問失敗的頁面:
connected from: ('127.0.0.1', 35590) ***************************************************HTTP/1.1 404 NotFound<!DOCTYPE html> <html> <head><title></title> <meta http-equiv="content-type" content="text/html"; charset="utf-8"/> </head> <body> <center><h2>所訪問的路徑不存在!</h2></center> </br> <!--打開網頁圖片--> <center><img src="https://image.freepik.com/free-vector/404-error-web-template-with-cow-flying-flat-style_23-2147777436.jpg" width="400px"/></center> </br></br> <center><h4><a href="http://blog.csdn.net/wobeatit/article/details/78641032">LQ的博客</a></h4><center> </body> </html>此時瀏覽器端會返回訪問失敗的頁面:
總結
以上是生活随笔為你收集整理的利用python编写设计多线程web服务器(计算机网络_自顶向下第六版_第二章1和4的编程作业)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拷贝构造函数的调用以及浅拷贝与深拷贝的理
- 下一篇: Codewars 开篇