这是关于chunk encoding传输以前相关传输编码的处理。没有做压缩解码的处理。

import tornado.ioloop
import tornado.iostream
import socket class WebRequest(object): LINE_END = b'\r\n'
def __init__(self, host, port=80, callback=None):
self.host = host
self.port =port
self.headers = {}
self.data = ''
self.callback = callback
self.transfer = 'stream'
self.http_ver_with_status = '' s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
self.stream = tornado.iostream.IOStream(s)
self.stream.connect((self.host, self.port), self.send_request) def send_request(self):
self.stream.write(b"GET / HTTP/1.0\r\nHost: %s\r\n\r\n" %self.host)
self.stream.read_until(self.LINE_END*2, self.on_headers) def on_headers(self, data):
lines = data.split(self.LINE_END)
self.http_ver_with_status = lines.pop(0)
for line in lines:
parts = line.split(b":", 1)
if len(parts) == 2:
self.headers[parts[0].strip()] = parts[1].strip()
if 'Content-Length' in self.headers:
if int(self.headers[b"Content-Length"]) > 0:
self.transfer = 'content_length'
self.stream.read_bytes(int(self.headers[b"Content-Length"]), self.on_body)
else:
self.callback(self)
self.stream.close() elif self.headers.get(b'Transfer-Encoding') == 'chunked':
self.transfer = 'chunked'
self.on_chunk()
else:
self.transfer = 'stream'
callback = lambda data: self.on_stream(data, True)
streaming_callback = lambda data: self.on_stream(data, False)
self.stream.read_until_close(callback, streaming_callback) def on_body(self, data):
self.data += data
self.callback(self)
self.stream.close() def on_chunk(self):
self.stream.read_until_regex(self.LINE_END, self.on_chunk_header) def on_chunk_header(self, data):
data = data.strip(self.LINE_END)
length = int(data, 16)
if length > 0:
self.stream.read_bytes(length+len(self.LINE_END), self.on_chunk_data)
else:
self.stream.read_bytes(length+len(self.LINE_END), self.on_chunk_end) def on_chunk_end(self, data):
self.callback(self)
self.stream.close() def on_chunk_data(self, data):
self.data += data
self.on_chunk() def on_stream(self, data, finish=False):
# do some thing
if finish: self.data += data
self.callback(self)
self.stream.close()
else:
self.data += data def callback(req):
print req.http_ver_with_status
print "Transfer :" , req.transfer
print "Headers: ", req.headers
#print "Data: ", req.data
req = WebRequest('cn.bing.com', 80, on_bing) def on_bing(req):
print req.http_ver_with_status
print "Transfer :" , req.transfer
print "Headers: ", req.headers
#print "Data: ", req.data
tornado.ioloop.IOLoop.instance().stop() if __name__ == '__main__':
req = WebRequest('valpha.gameloft.com', 20000, callback)
tornado.ioloop.IOLoop.instance().start()

Python Tornado简单的http request的更多相关文章

  1. Python 实现简单的 Web

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

  2. python超简单的web服务器

    今天无意google时看见,心里突然想说,python做web服务器,用不用这么简单啊,看来是我大惊小怪了. web1.py   1 2 3 #!/usr/bin/python import Simp ...

  3. Python 利用Python编写简单网络爬虫实例3

    利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...

  4. Python 利用Python编写简单网络爬虫实例2

    利用Python编写简单网络爬虫实例2 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://www.51testing. ...

  5. Python实现简单HTTP服务器

    Python实现简单HTTP服务器(一) 一.返回固定内容 复制代码 coding:utf-8 import socket from multiprocessing import Process de ...

  6. 用 python实现简单EXCEL数据统计

    任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...

  7. python开启简单webserver

    python开启简单webserver linux下面使用 python -m SimpleHTTPServer 8000 windows下面使用上面的命令会报错,Python.Exe: No Mod ...

  8. python tornado websocket 多聊天室(返回消息给部分连接者)

    python tornado 构建多个聊天室, 多个聊天室之间相互独立, 实现服务器端将消息返回给相应的部分客户端! chatHome.py // 服务器端, 渲染主页 --> 聊天室建立web ...

  9. Python开发简单爬虫 - 慕课网

    课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序   课程目录 第1章 课程介绍 ...

随机推荐

  1. loj125 除数函数求和 2

    https://loj.ac/problem/125 $原式=2\sum_{i=1}^n(i^2*{\lfloor}{\frac{n}{i}}{\rfloor})+3\sum_{i=1}^n(i*{\ ...

  2. 1-2JDK的安装和环境变量设置

    JDK的下载和安装 可以去oracle官网里面下载JDK:http://www.oracle.com 英文不好的同学可以通过下面这个百度网盘的链接进行下载:http://pan.baidu.com/s ...

  3. ASP.NET Core Action 读取流

    以前mvc5 action可以直接使用 var stream = HttpContext.Current.Request.InputStream; 读取流,在Core中有所不同,可以使用以下方式读取 ...

  4. Apache Cordova

    http://cordova.apache.org/ Apache Cordova is a platformfor building native mobile applications using ...

  5. JVM内存各个区域分工简单介绍

    JVM内存各个区域简单介绍: 程序计数器:程序计数器是一块较小的内存空间,它可以看作是当前线程所执行的字节码的行号指示器. 在使用多线程时,为了线程切换后能恢复到正确的执行位置,每条线程都需要有个独立 ...

  6. cordova应用使用手机调试

    对于cordova应用的调试,最方便调试方式还是作为h5应用在浏览器来调试,调试好了再打包cordova应用和打包apk.然而h5应用时的效果跟最终在安卓手机运行还有少数情况会不一样,因此,也需要有能 ...

  7. laravel核心思想

    服务容器.依赖注入.门脸模式 服务容器 容器概念 用来装一个个实例的对象,比如邮件类. IOC控制反转 IOC(Inversion of Control)控制反转,面向对象,可降低代码之间的耦合度,借 ...

  8. ZigBee cc2530芯片学习 error记录(1)

    ZigBee cc2530芯片学习 error记录   Error[e46]: Undefined external "LcdInit" referred in main( xxx ...

  9. 四次元新浪微博客户端Android源码

    四次元新浪微博客户端Android源码 源码下载:http://code.662p.com/list/11_1.html [/td][td] [/td][td] [/td][td] 详细说明:http ...

  10. Win2D 入门教程 VB 中文版

    继续填坑!又一个c#教程变为vb! 这是我翻译的Win2D教程,链接保留了微软原版的. 如果文档有问题,可以在 https://github.com/Nukepayload2/Win2dDocVB发 ...