今天无意google时看见,心里突然想说,python做web服务器,用不用这么简单啊,看来是我大惊小怪了.

web1.py

 
1
2
3
#!/usr/bin/python
import SimpleHTTPServer
SimpleHTTPServer.test()

web2.py

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/python
import SimpleHTTPServer
import SocketServer
import os
 
PORT = 80
WEBDIR = "f:/python语言学习"
 
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def translate_path(self, path):
        os.chdir(WEBDIR)
        return SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self,path)
 
try:
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    print "dir %s serving at port %s"%(repr(WEBDIR), PORT)
    httpd.serve_forever()
except:pass

web3.py , cgi server ,7777端口, 在web3.py执行目录下新建cgi-bin目录 , 在cgi-bin目录写hello.py

web3.py

 
1
2
3
4
5
from CGIHTTPServer import CGIHTTPRequestHandler  
from BaseHTTPServer import HTTPServer    
server_address=('',7777)  
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)  
httpd.serve_forever()  

hello.py

 
1
2
3
4
5
6
7
8
#!c:/Python24/python.exe
 
print "HTTP/1.0 200 OK"
print "Content-Type: text/html"
print ""
print "<p>"
print "Hello World!"
print "</p>"

以下这些是需要安装了 twisted 才能使用的
web4.py

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from twisted.web.resource import Resource                                      
from twisted.web import server                                                  
from twisted.web import static                                                  
from twisted.internet import reactor
                                                                                                              
                                                      
class ReStructured( Resource ):                                                                                                                                
   def __init__( self, filename, *a ):                                        
       self.rst = open( filename ).read( )                                                                                                              
                                                                                
   def render( self, request ):
       return self.rst              
PORT=8888                                                                                
 
resource = static.File('/')                                                  
resource.processors = { '.html'  : ReStructured }                              
resource.indexNames = [ 'index.html']                                  
                                                                                
reactor.listenTCP(                                                              
       PORT,                                                                  
       server.Site( resource )                                                
       )                                                                      
reactor.run( )  

web5.py, 这是又是支持cgi的,又是需要twisted模块的,也是需要在cgi-bin目录下执行,上边的hello.py也能用

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# -*- coding: utf-8 -*-
from twisted.internet import reactor
from twisted.web import static, server, twcgi
from twisted.web.resource import Resource
 
class Collection(Resource):
        def render_GET(self, request):
                return "hello world 你好"
 
root = static.File('./')
root.putChild('', Collection())
root.putChild('img', static.File('./img'))
root.putChild('cgi-bin', twcgi.CGIDirectory('cgi-bin'))
reactor.listenTCP(80, server.Site(root))
reactor.run()

当然,想实现复杂功能还是需要自己搞代码的,只不过想惊叹python的模块集成得太多功能了.
python超简单的web服务器。

python超简单的web服务器的更多相关文章

  1. Python超简单的HTTP服务器

    Python超简单的HTTP服务器 安装了python就可以 python -m SimpleHTTPServer 执行这一个命令即可实现一个HTTP服务器,将当前目录设为HTTP服务目录,可以通过h ...

  2. python一个简单的web服务器和客户端

    服务器:      当客户联系时创建一个连接套接字      从这个连接接收HTTP请求(*)      解释该请求所请求的特定文件      从服务器的文件系统获取该文件      并发送文件内容 ...

  3. Python实现简单的Web服务器 解析

    代码来源https://www.shiyanlou.com/courses/552,对它进行理解,注释 #-*- coding:utf-8 -*- import BaseHTTPServer clas ...

  4. 用Python建立最简单的web服务器

    利用Python自带的包可以建立简单的web服务器.在DOS里cd到准备做服务器根目录的路径下,输入命令: python -m Web服务器模块 [端口号,默认8000] 例如: python -m ...

  5. [转] 用Python建立最简单的web服务器

    [From] http://www.cnblogs.com/xuxn/archive/2011/02/14/build-simple-web-server-with-python.html 利用Pyt ...

  6. Python 实现简单的 Web

    简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...

  7. python自带的web服务器

    python自带的web服务器 python自带的包可以建立简单的web服务器 BaseHTTPServer 提供基本的web服务和处理类 SimpleHTTPServer 包含执行get请求的Sim ...

  8. 基于python2【重要】怎么自行搭建简单的web服务器

    基本流程:1.需要的支持     1)python本身有SimpleHTTPServer     2)ForkStaticServer.py支持,该文件放在python7目录下     3)将希望共享 ...

  9. 实现超简单的http服务器

    想在Linux下实现一个简单的web Server并不难.一个最简单的HTTP Server不过是一个高级的文件服务器,不断地接收客户端(浏览器)发送的HTTP请求,解析请求,处理请求,然后像客户端回 ...

随机推荐

  1. 【OGG】OGG基础知识整理

    [OGG]OGG基础知识整理 一.GoldenGate介绍 GoldenGate软件是一种基于日志的结构化数据复制软件.GoldenGate 能够实现大量交易数据的实时捕捉.变换和投递,实现源数据库与 ...

  2. 简单地总结几种常见web攻击手段及其防御方式

    web攻击手段有几种,本文简单介绍几种常见的攻击手段及其防御方式 XSS(跨站脚本攻击) CSRF(跨站请求伪造) SQL注入 DDOS XSS 概念 全称是跨站脚本攻击(Cross Site Scr ...

  3. Java自学手记——struts2

    struts2框架 struts2是一种基于MVC模式的框架,是在struts1的基础上融合了xwork的功能. struts2框架预处理了一些功能: >请求数据自动封装, >文件上传的功 ...

  4. Wiser的Junit测试用法

    package org.jbpm.process.workitem.email; import static org.junit.Assert.assertEquals; import static ...

  5. JVM常见垃圾回收算法

    jdk1.7.0_79 众所周知,Java是一门不用程序员手动管理内存的语言,全靠JVM自动管理内存,既然是自动管理,那必然有一个垃圾内存的回收机制或者回收算法.本文将介绍几种常见的垃圾回收(下文简称 ...

  6. js实现存储对象的数据结构hashTable和list

    以下代码是typescript语言来写的,其实和es6面向对象的写法基本一致.大家阅读后都明白这些方法的作用. hash hash结构用于处理和表现类似key/value的键值对,其中key通常可用来 ...

  7. 在ubuntu下使用minicom连接交换机/路由器Console口

    首先下载并安装minicom sudo apt-get install minicom 然后将Console线一端连上交换机的Console口,另一端连上电脑(我这里是通过USB口连接的) 然后查看对 ...

  8. 关于springmvc配置validator的注意事项

    <mvc:annotation-driven validator="validator" /> 在使用springmvc整合hibernate-validator做表单 ...

  9. HDU 6035---Colorful Tree(树形DP)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  10. 会话跟踪技术之——cookie

    1.cookieForm <%@ page language="java" contentType="text/html; charset=UTF-8" ...