最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年06月01日 15:05:25   作者:煙雨流年  
今天小編就為大家分享一篇python 編寫(xiě)簡(jiǎn)單網(wǎng)頁(yè)服務(wù)器的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

IDE:Pycharm

sever.py

#!/bin/python
#-*- coding: UTF-8 -*-
#文件名:server.py
#create by wzh 2017/10/26
import socket #導(dǎo)入socket模塊
import re
from 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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

武定县| 启东市| 格尔木市| 民勤县| 满洲里市| 鄢陵县| 尉氏县| 邵阳市| 宾川县| 萨嘎县| 顺昌县| 平远县| 百色市| 甘南县| 华宁县| 历史| 兰考县| 哈巴河县| 顺平县| 丽水市| 兰考县| 黄梅县| 东辽县| 台南市| 抚远县| 通化县| 徐汇区| 峨边| 盈江县| 福安市| 新竹市| 勐海县| 佛冈县| 南江县| 石狮市| 岳普湖县| 简阳市| 锡林浩特市| 将乐县| 竹溪县| 保康县|