12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- """
- @author: luojunhui
- """
- import hashlib
- from requests import RequestException
- from tenacity import (
- stop_after_attempt,
- wait_exponential,
- retry_if_exception_type,
- )
- def str_to_md5(strings):
- """
- 字符串转化为 md5 值
- :param strings:
- :return:
- """
- # 将字符串转换为字节
- original_bytes = strings.encode("utf-8")
- # 创建一个md5 hash对象
- md5_hash = hashlib.md5()
- # 更新hash对象,传入原始字节
- md5_hash.update(original_bytes)
- # 获取16进制形式的MD5哈希值
- md5_value = md5_hash.hexdigest()
- return md5_value
- def proxy():
- """
- 快代理
- """
- # 隧道域名:端口号
- tunnel = "j685.kdltps.com:15818"
- # 用户名密码方式
- username = "t14070979713487"
- password = "hqwanfvy"
- proxies = {
- "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel},
- "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel}
- }
- return proxies
- def request_retry(retry_times, min_retry_delay, max_retry_delay):
- """
- :param retry_times:
- :param min_retry_delay:
- :param max_retry_delay:
- """
- common_retry = dict(
- stop=stop_after_attempt(retry_times),
- wait=wait_exponential(min=min_retry_delay, max=max_retry_delay),
- retry=retry_if_exception_type((RequestException, TimeoutError)),
- reraise=True # 重试耗尽后重新抛出异常
- )
- return common_retry
|