国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python 編寫(xiě)簡(jiǎn)單網(wǎng)頁(yè)服務(wù)器的實(shí)例

2020-02-15 21:36:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

IDE:Pycharm

sever.py

#!/bin/python#-*- coding: UTF-8 -*-#文件名:server.py#create by wzh 2017/10/26import socket #導(dǎo)入socket模塊import refrom multiprocessing import Process #導(dǎo)入進(jìn)程模塊#設(shè)置靜態(tài)文件根目錄HTML_ROOT_DIR='./html'def handle_client(client_socket): """處理客戶端連接請(qǐng)求""" 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) #提取用戶請(qǐng)求的文件名 file_name=re.match(r"/w+ +(/[^ ]*) ",str(request_start_line)).group(1) if "/" == file_name:  file_name='/index.html' #打開(kāi)文件,讀取內(nèi)容 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__":   #如果直接運(yùn)行本文件,那么__name__為_(kāi)_main__(此時(shí)才運(yùn)行下面的程序),否則為對(duì)應(yīng)包名 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 創(chuàng)建socket對(duì)象 s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) #host = socket.gethostname() # 獲取本地主機(jī)名 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快捷鍵生成函數(shù)  handle_client_process.start()  c.close()

index.html

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>My Web</title></head><h1 align="center">welcome!</h1><p align="center">這是一個(gè)神奇的網(wǎng)站!</p><body></body></html>

運(yùn)行server.py

在瀏覽器中輸入localhost:1212

以上這篇python 編寫(xiě)簡(jiǎn)單網(wǎng)頁(yè)服務(wù)器的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林站長(zhǎng)站。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 西充县| 淮南市| 资阳市| 贵州省| 宜黄县| 长兴县| 犍为县| 洛阳市| 古田县| 太保市| 东平县| 玛曲县| 蕲春县| 重庆市| 宝应县| 江都市| 江口县| 城步| 冷水江市| 宁晋县| 钟山县| 锡林浩特市| 泰安市| 伊金霍洛旗| 大同县| 罗山县| 兴化市| 洪江市| 黔西县| 富宁县| 绵竹市| 樟树市| 辰溪县| 天峨县| 宁远县| 吉首市| 高平市| 荥阳市| 田东县| 海南省| 桐城市|