Python基于twisted实现简单的web服务器

yipeiwu_com5年前服务器

本文实例讲述了Python基于twisted实现简单的web服务器,分享给大家供大家参考。具体方法如下:

1. 新建htm文件夹,在这个文件夹中放入显示的网页文件

2. 在htm文件夹的同级目录下,建立web.py,web.py的内容为:

from twisted.web.resource import Resource 
from twisted.web import server 
from twisted.web import static 
from twisted.internet import reactor 
 
PORT = 1234 
 
######################################################################## 
class ReStructed(Resource): 
  """""" 
 
  #---------------------------------------------------------------------- 
  def __init__(self, filename, *a): 
    """Constructor""" 
    self.rst = open(filename).read() 
  def render(self, request): 
    return self.rst 
   
resource = static.File('htm/') 
resource.processors = {'.html':ReStructed} 
resource.indexNames = ['index.html'] 
 
reactor.listenTCP(PORT, server.Site(resource)) 
reactor.run() 

3. 安装上twisted 下载地址为:http://twistedmatrix.com/trac/

安装上zope模块:http://old.zope.org/Products/ZopeInterface/3.3.0/zope.interface-3.3.0.tar.gz/swreleasefile_view

5.在命令行中(windows系统)运行:python web.py

6.在浏览器中输入:127.0.0.1:1234,看到效果如下图所示:

希望本文所述对大家的Python程序设计有所帮助

相关文章

python下paramiko模块实现ssh连接登录Linux服务器

本文实例讲述了python下paramiko模块实现ssh连接登录Linux服务器的方法。分享给大家供大家参考。具体分析如下: python下有个paramiko模块,这个模块可以实现s...

Python探索之实现一个简单的HTTP服务器

Python标准库中的BaseHTTPServer模块实现了一个基础的HTTP服务器基类和HTTP请求处理类。这在文章python探索之BaseHTTPServer-实现Web服务器介绍...

windows服务器中检测PHP SSL是否开启以及开启SSL的方法

一、检测服务器是否开启了SSL 复制代码 代码如下:<?phpphpinfo();?>检查页面的openssl栏目,如果该栏目的OpenSSL support的值为enabl...

Python实现简单http服务器

Python实现简单http服务器

写一个python脚本,实现简单的http服务器功能: 1.浏览器中输入网站地址:172.20.52.163:20014 2.server接到浏览器的请求后,读取本地的index.htm...

Python实现简单的代理服务器

本文实例讲述了Python实现简单的代理服务器。分享给大家供大家参考。具体如下: 具备简单的管理功能,运行后 telnet localhost 9000 端口可以进行管理主要功能就是做包...