分类: python |
  • 1

Windows安装64位NumPy和SciPy包

 到这个网站下载whl文件

http://www.lfd.uci.edu/~gohlke/pythonlibs/

注意下载和自己的python对应的版本

如何安装whl文件:

pip install whatever.whl

查看更多...

分类:python | 固定链接 | 评论: 0 | 查看次数: 75497

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()

查看更多...

分类:python | 固定链接 | 评论: 0 | 查看次数: 54594

python hash函数

 hash计算可以把任意数据变成一段“摘要”,只要这段数据中的任何字节变化都会引起hash值非常大的变化,所以hash值可以用来检查数据有没有被篡改。hash计算的另外一个特性是hash值无法反向计算,哪怕你知道了hash值,你也不能通过某种算法反解出他原来的值。所以在WEB应用开发中也经常会用来加密用户的密码数据。在python的开发中当然也会需要这样的功能。本文就以最常用的两种算法md5和sha1为例简单介绍一下python的hash函数。

查看更多...

Tags: hash python

分类:python | 固定链接 | 评论: 0 | 查看次数: 11778

写Python的时候,经常会出现一个 错误,就是'ascii' codec can't encode character。这是因为Python是基于ascii处理字符的,如果你当前处理的字符串当中存在非ascii字符,就会报这个错误。

解决这个问题的方法很简单。在python的脚本文件的头部加入如下代码即可。
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

查看更多...

Tags: python 编码

分类:python | 固定链接 | 评论: 0 | 查看次数: 33389
  • 1