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

yipeiwu_com6年前服务器

本文实例讲述了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实现的简单文件传输服务器和客户端

还是那个题目(题目和流程见java版本),感觉光用java写一点新意也没有,恰巧刚学习了python,何不拿来一用,呵呵: 服务器端: import SocketServer, t...

python操作ssh实现服务器日志下载的方法

本文实例讲述了python操作ssh实现服务器日志下载的方法。分享给大家供大家参考。具体实现方法如下: #coding:utf-8 """ ssh操作例子 实现了服务器日志下载...

python实现tail实时查看服务器日志示例

我就废话不多说了,直接上代码吧! import paramiko from paramiko_expect import SSHClientInteraction host =...

Session服务器配置指南与使用经验的深入解析

一.摘要所有Web程序都会使用Session保存数据. 使用独立的Session服务器可以解决负载均衡场景中的Session共享问题.本文介绍.NET平台下建立Session服务器的几种...

本地文件上传到七牛云服务器示例(七牛云存储)

复制代码 代码如下:# _*_ coding: utf-8 _*_#---------------------------------------#   程序:把本地...