-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.py
109 lines (86 loc) · 2.46 KB
/
config.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'''
lurpo 全局配置
'''
from .useragent import ua_list
import random
from datetime import datetime
import requests
import httpx
import os
import sys
__all__ = [
'RUNFILE',
'HTTP_ENGINE',
'PERSISTENCE_ENABLED',
'PERSISTENCE_PATH',
'PROXIES',
'VERIFY_PROXIES',
'VERIFY_PROXIES',
'get_header',
'get_proxies',
'logging',
'verify_proxies'
]
# runfile
RUNFILE = os.path.split(sys.argv[0])[0]
# HTTP引擎
HTTP_ENGINE = httpx
# 对象持久化
PERSISTENCE_ENABLED = False
# 对象持久化存储路径
PERSISTENCE_PATH = 'endurance.db'
# 代理池
PROXIES = []
# 是否验证代理池
VERIFY_PROXIES = False
# 自定义请求头
def get_header(ualist = ua_list) -> str:
'''`get_header` 生成一个UA头.
Args:
None : 无参数
Returns:
str : 返回一个UA头字符串即可
'''
return random.choice(ualist)
# 自定义代理池
def get_proxies():
'''获取代理,并将代理赋值到 `lupro.config.PROXIES`.
Args:
None : 无参数
Returns:
bool : 返回Ture
'''
raise NameError('Please rewrite `get_proxies` function.')
# 自定义日志打印
def logging(text) -> str:
'''自定义日志打印 `logging` 可以重写,返回带 `text` 的字符串即可.
Args:
`text` : str 日志
Returns:
str : 返回处理后的打印日志
'''
return "%s INFO %s" % (datetime.now(), text)
# 自定义代理验证
def verify_proxies(proxies : str) -> str:
''' 自定义代理验证 `verify_proxies` 可以重写,如果可用返回 `proxies` 否则返回 `False`.
Args:
`proxies` : str 代理
Returns:
str | bool : 如果可用返回 `proxies` 否则返回 `False`
'''
try:
if HTTP_ENGINE.__name__ == 'requests':
HTTP_ENGINE.get(url='http://www.baidu.com/', headers={"User-Agent": get_header()}, proxies = {'http': f"//{proxies}"}, timeout=10)
elif HTTP_ENGINE.__name__ == 'httpx':
HTTP_ENGINE.get(url='http://www.baidu.com/', headers={"User-Agent": get_header()}, proxies = {'http://': f"http://{proxies}"}, timeout=10)
except:
return False
return proxies
# 载入配置
try:
import lupro_config
for i in __all__:
if hasattr(lupro_config, i):
locals()[i] = getattr(lupro_config, i)
except ModuleNotFoundError:
pass