首页 技术 正文
技术 2022年11月18日
0 收藏 464 点赞 4,595 浏览 989 个字

s.py

import time
import SimpleHTTPServer
import SocketServerBYTES_PER_SECOND=160*1024class MyHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
"""Serve a GET request."""
f = self.send_head()
if f:
self.copyfileobj(f, self.wfile)
f.close()
def copyfileobj(self,fsrc, fdst, length=16*1024):
"""copy data from file-like object fsrc to file-like object fdst"""
count = 0
t1 = time.time()
while 1:
buf = fsrc.read(length)
if not buf:
break
fdst.write(buf)
count += len(buf)
if count >= BYTES_PER_SECOND:
count = 0
delay = 1.0 - (time.time() - t1)
if delay > 0.0:
time.sleep(delay)
t1 = time.time()
PORT = 8000
Handler = MyHTTPRequestHandler
httpd = SocketServer.TCPServer(("127.0.0.1", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()

yes,that’s it! 总共三十几行代码。BYTES_PER_SECOND用于指定每秒传输的最大字节数。127.0.0.1 是服务器监听的IP。8000为http server监听的port。

这就是为什么写python程序是一种享受的原因。号称battery include的python,人用人爱的python。

可是python也由此给人一种误解,仅仅能写功能简单的程序。写不了高大上的程序,事实上真的是一种非常大的误解。

配合上DNSserver,这个30行的程序能够作为一个不错的升级測试环境。

另外,顺便说一句,假设是在linux以下,port号假设小于1000的话执行程序是要系统权限的,要不然会抛异常。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899