forked from doraemonext/wechat-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
93 lines (73 loc) · 2.09 KB
/
utils.py
File metadata and controls
93 lines (73 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
import io
import six
import time
import random
def to_text(value, encoding='utf-8'):
"""将 value 转为 unicode,默认编码 utf-8
:param value: 待转换的值
:param encoding: 编码
"""
if not value:
return ''
if isinstance(value, six.text_type):
return value
if isinstance(value, six.binary_type):
return value.decode(encoding)
return six.text_type(value)
def to_binary(value, encoding='utf-8'):
"""将 values 转为 bytes,默认编码 utf-8
:param value: 待转换的值
:param encoding: 编码
"""
if not value:
return b''
if isinstance(value, six.binary_type):
return value
if isinstance(value, six.text_type):
return value.encode(encoding)
if six.PY3:
return six.binary_type(str(value), encoding) # For Python 3
return six.binary_type(value)
def disable_urllib3_warning():
"""
https://urllib3.readthedocs.org/en/latest/security.html#insecurerequestwarning
InsecurePlatformWarning 警告的临时解决方案
"""
try:
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
except Exception:
pass
def generate_timestamp():
"""生成 timestamp
:return: timestamp string
"""
return int(time.time())
def generate_nonce():
"""生成 nonce
:return: nonce string
"""
return random.randrange(1000000000, 2000000000)
def convert_ext_to_mime(extension):
"""将扩展名转换为 MIME 格式
:return: mime string
"""
table = {
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'amr': 'audio/amr',
'mp3': 'audio/mpeg',
'mp4': 'video/mp4',
}
if extension in table:
return table[extension]
raise ValueError("Invalid extension in MIME table")
def is_allowed_extension(extension, type='upload_media'):
"""检查扩展名是否是可以上传到服务器
:return: True if ok
"""
table = ('jpg', 'jpeg', 'amr', 'mp3', 'mp4')
if extension in table:
return True
return False