这是关于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. ssh公私密钥的生成

    ssh密钥的生成 root账号密钥的生成: 这里我们切换到root账号下,执行ssh-keygen命令: ssh-keygen -t dsa 然后一路回车即可 """ [ ...

  2. Secrets CodeForces - 333A

    Secrets CodeForces - 333A 题意:这个世界上只有这样面值的硬币:1,3,9,27,81,...有一个商人,某一天遇到了一个顾客,他购买了价值n的商品,发现用自己的硬币无法付给商 ...

  3. npm install error: MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”

    When I tried to run angular 4 material2 demo on my windows server 2012, got a error message: node-pr ...

  4. Super Mario 树状数组离线 || 线段树

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. Python打开目录下所有文件

    用Python打开指定目录下所有文件,统计文件里特定的字段信息. 这里是先进入2017-02-25到2017-03-03目录,然后进入特定IP段目录下,最后打开文件进行统计 import os, gl ...

  6. android开发学习 ------- 【转】Genymotion 小白安装

    参考 https://www.cnblogs.com/whycxb/p/6850454.html 很详细,全面

  7. 009全志R16平台tinav3.0下编译不过的问题

    009全志R16平台tinav3.0下编译不过的问题 2018/11/13 11:39 版本:V1.0 开发板:SC3817R SDK:tina v3.0 1.01原始编译全志r16平台tinav3. ...

  8. VS Code使用技巧整理

    转自:https://blog.csdn.net/u011127019/article/details/58586129 https://blog.csdn.net/sgdd123/article/d ...

  9. FPGA开发中的脚本语言

    多数FPGA开发者都习惯图形化界面(GUI).GUI方式简单易学,为小项目提供了一键式流程.然而,随着FPGA项目越来越复杂,在很多情况下GUI工具就阻碍了工作效率.因为GUI工具不能对整个开发过程提 ...

  10. linux环境iptables配置

    Linux iptables常用规则 设置一个自己用的表, 允许ping 允许ssh 允许 web 允许mysql 允许 ftp 允许dns查询 其他的拒绝.脚本如下 # Firewall confi ...