日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python:实现简单的web开发demo

發布時間:2025/3/20 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python:实现简单的web开发demo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

server.py代碼:

#!/bin/python #-*- coding: UTF-8 -*- #文件名:server.py #create by wzh 2017/10/26import 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_bodyprint("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 = 7000 ##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()

同文件夾下html代碼:

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

cmd指令:
python -m http.server 7000

總結

以上是生活随笔為你收集整理的python:实现简单的web开发demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。