python socket实现一个http请求 支持gzip压缩

python用socket实现发送和接收一个http请求,自动处理gzip。直接上代码。

# -*- coding: utf-8 -*-

import zlib, socket

conn = socket.create_connection( ("www.zeroplace.cn", 80) );

data = "''GET / HTTP/1.1
Host: www.zeroplace.cn
Accept-Encoding: gzip, deflate
'''

print data

conn.send(data + "\r\n")

recv_data = ""
header = None
body = None
length = 0

headers = {}

buf = conn.recv(1024)

html = ""

while True:
	recv_data += buf
	
	if header is None:
		index = recv_data.find("\r\n\r\n")
		if index >= 0:
			header = recv_data[0:index];
			recv_data = recv_data[index+4:];
			header_lines = header.split("\r\n")
			status_line = header_lines[0]
			print status_line
			for line in header_lines[1:]:
				print line
				line = line.strip("\r\n")
				if len(line) == 0:
					continue
				colonIndex = line.find(":")
				fieldName = line[:colonIndex]
				fieldValue = line[colonIndex+1:].strip()
				headers[fieldName] = fieldValue

			# print headers
			length = int(headers['Content-Length'])

	if header is not None and len(recv_data) >= length:
		break;
	else:
		buf = conn.recv(1024)

if 'Content-Encoding' in headers and headers['Content-Encoding'] == 'gzip' :
	html = zlib.decompress(recv_data, 16+zlib.MAX_WBITS)
else:
	html = recv_data

print html

conn.close()




有什么问题可以加群,100852896
点击这里加入此群 在线提问
文章来自: 本站原创
Tags:
评论: 0 | 查看次数: 50073
博主QQ: 友情链接请找我
QQ群: 灰色档案
返回顶部 关闭