Python Web靜態伺服器 非堵塞模式

2021-09-26 13:37:51 字數 4515 閱讀 4768

#coding=utf-8

from socket import *

import time

# 用來儲存所有的新鏈結的socket

g_socket_list = list()

def main():

server_socket = socket(af_inet, sock_stream)

server_socket.setsockopt(sol_socket, so_reuseaddr , 1)

server_socket.bind(('', 7890))

server_socket.listen(128)

# 將套接字設定為非堵塞

# 設定為非堵塞後,如果accept時,恰巧沒有客戶端connect,那麼accept會

# 產生乙個異常,所以需要try來進行處理

server_socket.setblocking(false)

while true:

# 用來測試

time.sleep(0.5)

try:

newclientinfo = server_socket.accept()

except exception as result:

pass

else:

print("乙個新的客戶端到來:%s" % str(newclientinfo))

newclientinfo[0].setblocking(false) # 設定為非堵塞

for client_socket, client_addr in g_socket_list:

try:

recvdata = client_socket.recv(1024)

if recvdata:

print('recv[%s]:%s' % (str(client_addr), recvdata))

else:

print('[%s]客戶端已經關閉' % str(client_addr))

client_socket.close()

g_socket_list.remove((client_socket,client_addr))

except exception as result:

pass

print(g_socket_list) # for test

if __name__ == '__main__':

main()

import time

import socket

import sys

import re

class wsgiserver(object):

"""定義乙個wsgi伺服器的類"""

def __init__(self, port, documents_root):

# 1. 建立套接字

self.server_socket = socket.socket(socket.af_inet, socket.sock_stream)

# 2. 繫結本地資訊

self.server_socket.setsockopt(socket.sol_socket, socket.so_reuseaddr, 1)

self.server_socket.bind(("", port))

# 3. 變為監聽套接字

self.server_socket.listen(128)

self.server_socket.setblocking(false)

self.client_socket_list = list()

self.documents_root = documents_root

def run_forever(self):

"""執行伺服器"""

# 等待對方鏈結

while true:

# time.sleep(0.5) # for test

try:

new_socket, new_addr = self.server_socket.accept()

except exception as ret:

print("-----1----", ret) # for test

else:

new_socket.setblocking(false)

for client_socket in self.client_socket_list:

try:

request = client_socket.recv(1024).decode('utf-8')

except exception as ret:

print("------2----", ret) # for test

else:

if request:

self.deal_with_request(request, client_socket)

else:

client_socket.close()

self.client_socket_list.remove(client_socket)

print(self.client_socket_list)

def deal_with_request(self, request, client_socket):

"""為這個瀏覽器伺服器"""

if not request:

return

request_lines = request.splitlines()

for i, line in enumerate(request_lines):

print(i, line)

# 提取請求的檔案(index.html)

print("正則提取資料:", ret.group(1))

print("正則提取資料:", ret.group(2))

file_name = ret.group(2)

if file_name == "/":

file_name = "/index.html"

# 讀取檔案資料

try:

f = open(self.documents_root+file_name, "rb")

except:

response_body = "file not found, 請輸入正確的url"

# 將header返回給瀏覽器

client_socket.send(response_header.encode('utf-8'))

# 將body返回給瀏覽器

# 將header返回給瀏覽器

client_socket.send( response_header.encode('utf-8') + response_body)

# 設定伺服器服務靜態資源時的路徑

documents_root = "./html"

def main():

"""控制web伺服器整體"""

# python3 ***x.py 7890

if len(sys.ar**) == 2:

port = sys.ar**[1]

if port.isdigit():

port = int(port)

else:

print("執行方式如: python3 ***.py 7890")

return

print("http伺服器使用的port:%s" % port)

Python Web靜態伺服器 gevent版

from gevent import monkey import gevent import socket import sys import re monkey.patch all class wsgiserver object 定義乙個wsgi伺服器的類 def init self,port,d...

pythonweb靜態伺服器 靜態Web伺服器

靜態web伺服器 可以發出請求的瀏覽器提供靜態文件的程式。搭建python自帶靜態web伺服器 如何搭建python自帶的靜態web伺服器 windows可以實現資源共享的目的 語法 python m http.server 埠號 1.win r 輸入cmd進入命令指令視窗 2.切換目錄,選擇目錄建...

非阻塞web靜態伺服器

單程序但鏈結非阻塞web靜態伺服器 author bre athy contact productware pycharm file 單程序但鏈結非阻塞web靜態伺服器.py time 2019 11 9 5 33 import socket,re defservice client new soc...