欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python靜態(tài)web服務(wù)器實(shí)現(xiàn)方法及代碼詳解

 更新時(shí)間:2022年11月20日 11:04:15   投稿:laozhang  
在本篇內(nèi)容里小編給大家分享了一篇關(guān)于python靜態(tài)web服務(wù)器實(shí)現(xiàn)方法,有需要的朋友們可以參考下。

1、編寫TCP服務(wù)器程序。

2、獲取瀏覽器發(fā)送的http請(qǐng)求消息數(shù)據(jù)。

3、讀取固定的頁(yè)面數(shù)據(jù),將頁(yè)面數(shù)據(jù)組裝成HTTP響應(yīng)消息數(shù)據(jù)并發(fā)送給瀏覽器。

4、HTTP響應(yīng)報(bào)文數(shù)據(jù)發(fā)送完成后,關(guān)閉服務(wù)于客戶端的套接字。

實(shí)例

# 時(shí)間: 2021/10/21 20:38
import socket
 
 
if __name__ == '__main__':
    # 創(chuàng)建tcp服務(wù)端套接字
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 設(shè)置端口號(hào)復(fù)用, 程序退出端口立即釋放
    tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
    # 綁定端口號(hào)
    tcp_server_socket.bind(("", 8080))
    # 設(shè)置監(jiān)聽
    tcp_server_socket.listen(128)
    while True:
        # 等待接受客戶端的連接請(qǐng)求
        new_socket, ip_port = tcp_server_socket.accept()
        # 代碼執(zhí)行到此,說明連接建立成功
        recv_client_data = new_socket.recv(4096)
        # 對(duì)二進(jìn)制數(shù)據(jù)進(jìn)行解碼
        recv_client_content = recv_client_data.decode)
        print(recv_client_content)
        
 
        # 響應(yīng)行
        response_line = "HTTP/1.1 200 OK\r\n"
        # 響應(yīng)頭
        response_header = "Server: py1.0\r\n"
 
        # 響應(yīng)體
        response_body = "Hello,guys!"
 
        # 拼接響應(yīng)報(bào)文
        response_data = (response_line + response_header + "\r\n"+ response_body).encode()
        # 發(fā)送數(shù)據(jù)
        new_socket.send(response_data)
 
        # 關(guān)閉服務(wù)與客戶端的套接字
        new_socket.close()

實(shí)例擴(kuò)展:

import socket
import re
import time


def service_client(new_socket):
 """為這個(gè)客戶端服務(wù)"""

 # 1.接收瀏覽器發(fā)送過來(lái)的請(qǐng)求,即http請(qǐng)求
 # GET / HTTP/1.1
 # --------
 request = new_socket.recv(1024).decode('utf-8')

 # 判斷客戶端意外斷開鏈接返回空字符串
 if not request:
  # 關(guān)閉套接字并退出
  new_socket.close()
  print("==="*30)
  return

 # 分隔套接字
 request_lines = request.splitlines()
 print()
 print(">"*20)
 print(request_lines)

 file_name = ""
 ret = re.match(r"[^/]+(/[^ ]*)", request_lines[0])
 if ret:
  file_name = ret.group(1)
  if file_name == "/":
   file_name = "/index.html"

 # 2.返回http格式數(shù)據(jù) 給瀏覽器
 try:
  f = open("./html" + file_name, "rb")
 except:
  response = "HTTP/1.1 404 NOT FOUND\r\n"
  response += "Content-Type:text/html;charset=utf-8\r\n"
  response += "\r\n"
  response += "<h1>404 not found <br> 沒有發(fā)現(xiàn)所請(qǐng)求資源</h1>"
  response += str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
  new_socket.send(response.encode('utf-8'))
 else:
  html_content = f.read()
  f.close()
  # 2.1準(zhǔn)備發(fā)送給瀏覽器的數(shù)據(jù)---header
  response = "HTTP/1.1 200 OK\r\n"
  response += "\r\n"
  # 2.2準(zhǔn)備發(fā)送給瀏覽器的數(shù)據(jù) ---body
  # 將response header發(fā)送給瀏覽器
  new_socket.send(response.encode("utf-8"))
  # 將response body發(fā)送給瀏覽器
  new_socket.send(html_content)

 # 關(guān)閉套接字
 new_socket.close()


def main():
 """用來(lái)完成整體的控制"""
 # 1.創(chuàng)建套接字
 tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 # 端口復(fù)用
 tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 # 2.綁定
 tcp_server_socket.bind(("", 7890))
 # 3.變?yōu)樘捉幼?
 tcp_server_socket.listen(128)

 while True:
  # 4.等待客戶端的鏈接
  new_socket, client_addr = tcp_server_socket.accept()

  print(client_addr)

  # 5.為這個(gè)客戶端服務(wù)
  service_client(new_socket)

 # 關(guān)閉監(jiān)聽套接字
 tcp_server_socket.close()


if __name__ == '__main__':
 main()

到此這篇關(guān)于python靜態(tài)web服務(wù)器實(shí)現(xiàn)方法及代碼詳解的文章就介紹到這了,更多相關(guān)python靜態(tài)web服務(wù)器如何實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論