Python WebDev
2017-01-30
这个组织下的每个项目单拿出来都是响当当的存在:
- Click — Beautiful, composable command line interface creation kit.
- Flask — a flexible and popular web development framework
- ItsDangerous — Safely pass trusted data to untrusted environments and back.
- Jinja — a full featured template engine for Python
- MarkupSafe — Safely add untrusted strings to HTML/XML markup.
- Quart — an async reimplementation of flask
- Werkzeug — The comprehensive WSGI web application library.
- Click 命令行工具
- Flask 轻量级 Web 框架
- ItsDangerous
- Jinja 知名模板库
- MackupSafe
- Quart
- Werkzeug Web 工具箱(WSGI),Flask 的底层
Python Unicode 字符编码
2016-06-22
源代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print u'中国'
报错:
Traceback (most recent call last):
File "test.py", line 4, in <module>
print u'中国'
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
Python
2016-05-16
-
bytes
按索引取值得到的是整数。
b'abc'[0]
97
-
str
转 bytes
只需要编码一下就行了。反过来就是解码一下。
s = '你好'
b = bytes(s, 'utf-8')
# b'\xe4\xbd\xa0\xe5\xa5\xbd'
assert b.decode('utf-8') == s
bytes('hello')
TypeError: string argument without an encoding
bytes('hello', 'ascii')
b'hello'
'hello'.encode()
b'hello'
bytes('hello', 'utf-16')
b'\xff\xfeh\x00e\x00l\x00l\x00o\x00'
bytes('hello', 'utf-32')
b'\xff\xfe\x00\x00h\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00'
-
bytes
和 int
列表的转换。
list(b'abc')
[97, 98, 99]
bytes([97, 98, 99])
b'abc'
bytes([256])
ValueError: bytes must be in range(0, 256)
# 大端序
(2008).to_bytes(length=4, byteorder='big', signed=False)
b'\x00\x00\x07\xd8'
# 小端序
(2008).to_bytes(length=4, byteorder='little', signed=False)
b'\xd8\x07\x00\x00'
struct.pack('<I', 2008)
b'\xd8\x07\x00\x00'
int.from_bytes(b'\x00\x00\x07\xd8', 'big')
2008
int.from_bytes(b'\xd8\x07\x00\x00', 'little')
2008
int.from_bytes(b'abc', 'little')
6513249
int.from_bytes(b'cba', 'big')
6513249
PS:2020/11/02, 字节顺序(大端序、小端序)
-
Python2 字符串
# python2
print(repr('你好'))
'\xc4\xe3\xba\xc3'
print('\xc4\xe3\xba\xc3')
你好
# python3
print('\xc4\xe3\xba\xc3')
'ÄãºÃ'
print('\xc4\xe3\xba\xc3'.encode('latin-1'))
b'\xc4\xe3\xba\xc3'
print('\xc4\xe3\xba\xc3'.encode('latin-1').decode('gbk'))
'你好'
Python
2016-02-03
import tempfile
import time
timestr = time.strftime("%Y%m%d%H%M%S")
临时文件
# tempfile.mktemp(suffix='', prefix='tmp', dir=None)
# tempfile.mkstemp(suffix=None, prefix=None, dir=None, text=False)
filepath_temp = tempfile.mktemp(suffix='.html', prefix='.cache_%s_' % timestr)
print(filepath_temp)
# /tmp/.cache_20210929205452_zlmi3pkf.html
filepath_temp = tempfile.mktemp(suffix='.html', prefix='.cache_%s_' % timestr,
dir='/opt/apps/markjour/tmp/')
print(filepath_temp)
# /opt/apps/markjour/tmp/.cache_20210929205452_mcr7nj3e.html
filepath_temp = tempfile.mkstemp(suffix='.html', prefix='.cache_%s_' % timestr, text=True)
mkstemp
和 mktemp
的区别:mkstemp
返回一个文件描述符,mktemp
返回一个文件路径。
mktemp
返回的路径理论上会被另一个进程使用,所以这是 UNSAFE 的,应该用 mkstemp
代替。
mktemp
从 Python 2.3 开始标记为 Deprecated,但是至今还是可以调用。
临时目录
# tempfile.mkdtemp(suffix=None, prefix=None, dir=None)
dirpath_temp = tempfile.mkdtemp()
print(dirpath_temp)
# /tmp/tmp1dj2cl0d
dirpath_temp = tempfile.mkdtemp(prefix='build_',
dir='/opt/apps/markjour/tmp/')
print(dirpath_temp)
# /opt/apps/markjour/tmp/build_9k5synh5
Python zip
2016-02-02
import zipfile
import os
zip_path = "/path/to/zip/file.zip"
extract_path = "/path/to/extract/directory"
if not os.path.exists(extract_path):
os.makedirs(extract_path)
with zipfile.ZipFile(zip_path, 'r') as fp:
# 直接解压
zip_ref.extractall(target_dir)
# 逐个解压
for filename in fp.namelist():
# 如果文件存在就先移除,避免 FileExistsError
extract_file_path = os.path.join(extract_path, filename)
if os.path.exists(extract_file_path):
# print("跳过解压缩文件:", filename)
# continue
os.remove(extract_file_path)
# 解压,会自动创建目录结构
fp.extract(filename, extract_path)
Python 字符编码
2016-01-23
关于 sys.setdefaultencoding
的一些问题。
Python
2016-01-21
.gitignore 常见的 *.py[cod]
。
参考 gitignore.io。
.py
.pyc
编译过后生成的字节码文件
.pyo
经过优化的字节码文件。Python2 时代常见,Python3 不再使用。现在是 .opt-1.pyc
, opt-2.pyc
PS:关于这个优化到底是做什么以后再好好研究。
.pyd
.pyi
Python Stub 文件,IDE 中为了提供更好的提示会带一些这样的文件,只是所有方法都是空的。
ps: 现在的 PyCharm 好像带的 stub 文件都是 py
后缀了。
.pyw
Windows 专供,py 文件由 python.exe 解析,pyw 文件由 pythonw.exe 执行
他们不同之处就是,python.exe 会导致一个命令行窗口的出现,而,pythonw.exe 不会,所以常用在 GUI 程序上。
.pth
自定义路径导入规则的文件,参考:.pth 文件和 site 模块
Python 压缩
2015-12-31
zlib
— Compression compatible with gzip
gzip
— Support for gzip files
bz2
— Support for bzip2 compression
lzma
— Compression using the LZMA algorithm
zipfile
— Work with ZIP archives
tarfile
— Read and write tar archive files
Python 编码规范
2015-12-01
pep8
是一个遵守 PEP8 编码规范的 Python 代码检测工具。
Python Tornado
2015-10-04
Tornado POST 请求直接 599 了,经过 WireShark 抓包发现根本没有发出请求。
最后竟然发现和版本有关,4.1 下才有问题,4.2 以后就好了。