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程序设计有所帮助

相关文章

linux系统使用python监控apache服务器进程脚本分享

crtrl.py监控Apache服务器进程的Python 脚本 复制代码 代码如下:!/usr/bin/env Python import os, sys, time while Tr...

PHP服务器页面间跳转实现方法

(注意不是用 header ,js 等方法做的客户端跳转) 复制代码 代码如下: function server_transfer($dest) { global ...; // 把希望...

利用php获取服务器时间的实现代码

很多时候我们喜欢用js来获取日期和时间,但这仅仅是客户端的。我们可以用php的date函数即可来获取服务器上的时间:复制代码 代码如下:<?php//将时区设置为中国date_de...

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

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

python 定时任务去检测服务器端口是否通的实例

如下所示: import socket import threading import time def testconn( host , port ): sk = sock...