Python實(shí)現(xiàn)簡易版的Web服務(wù)器(推薦)
下面給大家介紹python實(shí)現(xiàn)簡易版的web服務(wù)器,具體內(nèi)容詳情大家通過本文學(xué)習(xí)吧!

1、請(qǐng)自行了解HTTP協(xié)議
http://chabaoo.cn/article/133883.htm(點(diǎn)擊跳轉(zhuǎn))
2、創(chuàng)建Socket服務(wù),監(jiān)聽指定IP和端口

3、以阻塞方式等待客戶端連接

4、讀取客戶端請(qǐng)求數(shù)據(jù)并進(jìn)行解析

5、準(zhǔn)備服務(wù)器運(yùn)行上下文

6、處理客戶端請(qǐng)求數(shù)據(jù)

7、根據(jù)用戶請(qǐng)求路徑讀取文件

8、返回響應(yīng)結(jié)果給客戶端

9、程序入口

10、目錄結(jié)構(gòu)

11、運(yùn)行
python wsgiserver.py app:run
12、源碼
a.wsgiserver.py文件
#encoding:utf-8
import socket
import StringIO
import sys
import logging
from datetime import datetime
logger = logging.getLogger(__name__)
class WSGIServer(object):
address_family = socket.AF_INET
socket_type = socket.SOCK_STREAM
request_queue_size = 30
recv_size = 1024
def __init__(self, server_address):
self._listen_socket = _listen_socket = socket.socket(self.address_family,
self.socket_type) _listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1) _listen_socket.bind(server_address)
_listen_socket.listen(self.request_queue_size)
_host, _port = _listen_socket.getsockname()
self._server_name = socket.getfqdn(_host)
self._server_port = _port
self._headers_set = []
self._application = None
self._client = None
self._request_data = None
self._request_method = None
self._path = None
self._request_version = None
self._start_response = None
def set_application(self, application):
self._application = application
def server_forever(self):
_listen_socket = self._listen_socket
logger.info('listen on %s:%s', self._server_name, self._server_port) while 1:
try:
self._client, _addr = _listen_socket.accept()
self._handle_request(_addr)
except KeyboardInterrupt as e:
logger.info('interrupt')
break
except BaseException as e:
logger.error(e)
def _handle_request(self, client_addr):
self._request_data = _request_data = self._client.recv(self.recv_size)
self._parse_request_data(_request_data)
_env = self._get_environment(client_addr)
_result = self._application(_env, self.start_response)
self._finish_response(_result)
def _parse_request_data(self, request_data):
_request_line = str(request_data.splitlines()[0]).rstrip('\r\n')
(self._request_method, self._path, self._request_version) = _request_line.split()
def _get_environment(self, client_addr):
_env = {}
_env['wsgi.version'] = (1, 0)
_env['wsgi.url_scheme'] = 'http'
_env['wsgi.input'] = StringIO.StringIO(self._request_data) _env['wsgi.errors'] = sys.stderr
_env['wsgi.multithread'] = False
_env['wsgi.multiprocess'] = False
_env['wsgi.run_once'] = False
_env['REQUEST_METHOD'] = self._request_method.upper()
_env['PATH_INFO'] = self._path
_env['SERVER_NAME'] = self._server_name
_env['SERVER_PORT'] = self._server_port
_env['HTTP_CLIENT_IP'] = client_addr[0]
logger.info('%s %s %s %s', _env['HTTP_CLIENT_IP'], datetime.now().strftime('%Y-%m-%d %H:%M:%S'), _env['REQUEST_METHOD'], _env['PATH_INFO'])
return _env
def start_response(self, status, response_headers, exc_info=None): _server_headers = [
('Date', 'Sun, 7 Jun 2015 23:07:04 GMT'),
('Server', 'WSGIServer 0.1')
]
self._headers_set = [status, response_headers + _server_headers]
def _finish_response(self, result):
_status, _response_headers = self._headers_set
_response = 'HTTP/1.1 {status}\r\n'.format(status=_status) for _header in _response_headers:
_response += '{0}:{1}\r\n'.format(*_header)
_response += '\r\n'
for _data in result:
_response += _data
self._client.sendall(_response)
self._client.close()
def make_server(server_address, application):
server = WSGIServer(server_address)
server.set_application(application)
return server
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
server_addr= ('0.0.0.0', 43002)
app_path = sys.argv[1]
module, application = app_path.split(':')
module = __import__(module)
application = getattr(module, application)
httpd = make_server(server_addr, application)
httpd.server_forever()
b.app.py文件
#encoding:utf-8
import os
class PageNotFoundException(BaseException):
pass
def render(filename, dirname='html'):
_path = os.path.join(dirname, filename)
if os.path.exists(_path):
with open(_path, 'rb') as handler:
return handler.read()
raise PageNotFoundException('file not found:%s' % _path)
def run(env, start_response):
_path = env.get('PATH_INFO')
response = ''
try:
_path = 'index.html' if _path == '/' else _path[1:]
if _path.endswith('.css'):
start_response('200 OK', [('Content-Type', 'text/css')])
elif _path.endswith('.js'):
start_response('200 OK', [('Content-Type', 'text/javascript')]
elif _path.endswith('.html'):
start_response('200 OK', [('Content-Type', 'text/html')])
else:
start_response('200 OK', [('Content-Type', 'text/plain'), ('Content-Disposition', 'attachment; filename=%s' % os.path.basename(_path))])
response = render(_path)
except PageNotFoundException as e:
response = render('404.html')
return [response, '\r\n']
總結(jié)
以上所述是小編給大家介紹的Python實(shí)現(xiàn)簡易版的Web服務(wù)器,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Python Tornado實(shí)現(xiàn)WEB服務(wù)器Socket服務(wù)器共存并實(shí)現(xiàn)交互的方法
- Python Web靜態(tài)服務(wù)器非堵塞模式實(shí)現(xiàn)方法示例
- python3實(shí)現(xiàn)微型的web服務(wù)器
- Python面向?qū)ο笾甒eb靜態(tài)服務(wù)器
- python實(shí)現(xiàn)靜態(tài)web服務(wù)器
- Tornado Web Server框架編寫簡易Python服務(wù)器
- Python Web程序部署到Ubuntu服務(wù)器上的方法
- python快速建立超簡單的web服務(wù)器的實(shí)現(xiàn)方法
- python探索之BaseHTTPServer-實(shí)現(xiàn)Web服務(wù)器介紹
- Python 實(shí)現(xiàn)一個(gè)簡單的web服務(wù)器
相關(guān)文章
citespace數(shù)據(jù)處理:用python對(duì)Ref文檔進(jìn)行去重方式
這篇文章主要介紹了citespace數(shù)據(jù)處理:用python對(duì)Ref文檔進(jìn)行去重方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Python實(shí)現(xiàn)輕松找出兩個(gè)列表不同之處
在日常編程中,需要比較兩個(gè)列表并找出它們之間差異是一種常見需求,在本文中,我們將深入探討Python中查找兩個(gè)列表差異值的方法,需要的小伙伴可以參考下2023-12-12
python爬蟲基礎(chǔ)教程:requests庫(二)代碼實(shí)例
這篇文章主要介紹了python爬蟲基礎(chǔ)教程:requests庫(二),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Python爬取數(shù)據(jù)并實(shí)現(xiàn)可視化代碼解析
這篇文章主要介紹了Python爬取數(shù)據(jù)并實(shí)現(xiàn)可視化代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Matlab讀取excel并利用拉依達(dá)準(zhǔn)則篩選數(shù)據(jù)的全過程
在Excel中錄入好數(shù)據(jù)以后經(jīng)常需要被matlab讀取,具體該如何讀取并進(jìn)行篩選呢?下面這篇文章就來給大家介紹了關(guān)于Matlab讀取excel并利用拉依達(dá)準(zhǔn)則篩選數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2021-08-08
Python進(jìn)行ffmpeg推流和拉流rtsp、rtmp實(shí)例詳解
Python推流本質(zhì)是調(diào)用FFmpeg的推流進(jìn)程,下面這篇文章主要給大家介紹了關(guān)于Python進(jìn)行ffmpeg推流和拉流rtsp、rtmp的相關(guān)資料,需要的朋友可以參考下2023-01-01

