python超简单的web服务器
今天无意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服务器的更多相关文章
- Python超简单的HTTP服务器
Python超简单的HTTP服务器 安装了python就可以 python -m SimpleHTTPServer 执行这一个命令即可实现一个HTTP服务器,将当前目录设为HTTP服务目录,可以通过h ...
- python一个简单的web服务器和客户端
服务器: 当客户联系时创建一个连接套接字 从这个连接接收HTTP请求(*) 解释该请求所请求的特定文件 从服务器的文件系统获取该文件 并发送文件内容 ...
- Python实现简单的Web服务器 解析
代码来源https://www.shiyanlou.com/courses/552,对它进行理解,注释 #-*- coding:utf-8 -*- import BaseHTTPServer clas ...
- 用Python建立最简单的web服务器
利用Python自带的包可以建立简单的web服务器.在DOS里cd到准备做服务器根目录的路径下,输入命令: python -m Web服务器模块 [端口号,默认8000] 例如: python -m ...
- [转] 用Python建立最简单的web服务器
[From] http://www.cnblogs.com/xuxn/archive/2011/02/14/build-simple-web-server-with-python.html 利用Pyt ...
- Python 实现简单的 Web
简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...
- python自带的web服务器
python自带的web服务器 python自带的包可以建立简单的web服务器 BaseHTTPServer 提供基本的web服务和处理类 SimpleHTTPServer 包含执行get请求的Sim ...
- 基于python2【重要】怎么自行搭建简单的web服务器
基本流程:1.需要的支持 1)python本身有SimpleHTTPServer 2)ForkStaticServer.py支持,该文件放在python7目录下 3)将希望共享 ...
- 实现超简单的http服务器
想在Linux下实现一个简单的web Server并不难.一个最简单的HTTP Server不过是一个高级的文件服务器,不断地接收客户端(浏览器)发送的HTTP请求,解析请求,处理请求,然后像客户端回 ...
随机推荐
- 【知识整理】这可能是最好的RxJava 2.x 教程(完结版)
为什么要学 RxJava? 提升开发效率,降低维护成本一直是开发团队永恒不变的宗旨.近两年来国内的技术圈子中越来越多的开始提及 RxJava ,越来越多的应用和面试中都会有 RxJava ,而就目前的 ...
- webpackage 2.x 使用
webpackage 2.x 使用 安装---(在项目目录下) //1.初始化npm的配置(添加package.json) npm init //2.安装 webpackage npm install ...
- ionic项目结构解析
ionic项目结构解析 原始结构 创建一个IonicDemo项目 'ionic start IonicDemo sidemenu' 这种结构多模块开发比较麻烦,因为view跟controller分开路 ...
- 最近快速的过了一遍php基础语法
把in_array() 写成is_array() ;结果自己坑了自己一万:打脸一万下,先记账上
- HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配)
HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配) Description The zoo have N cats and M dogs, toda ...
- 微信小程序简述
最近在公司实习,经理要求做一个微信小程序,晚上闲时来写一下. 微信小程序问世没多久,但毋庸置疑的是在不久的将来,它可以替代掉很多的APP.个人认为它的优势在于占用资源少,可以做到即用即走,对于一些使用 ...
- java Script 用if else 实现从大到小指定输出,升序排列
我只是一个小白 各位大神看到不要介意 var a = Number(prompt("请输入你需要排列的第一个数字")) var b = Number(prompt("请输 ...
- DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate Date Dimension)
跟大家的交流是我的动力. :) DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate Date Dimension) 难度: ★☆☆☆☆(1星) 适用范围: ★ ...
- 本地存储之cookie、localStorage、sessionStorage
一.本地存储分为cookie,以及新增的localStorage和sessionStorage 1.cookie存储在本地,容量最大4k,在同源的http请求时携带传递,损耗带宽,可设置访问路径,只有 ...
- jenkins - MultiJob使用
我们如果使用jenkins需要由串行,并行,传递参数和等待执行的功能的话,那我们会用到jenkins里面的两个东西:MultiJob和pipeline 这里我介绍下MultiJob的使用 实例任务的拓 ...