web.py 2.5 KB
Newer Older
H
hjdhnx 已提交
1 2 3 4 5 6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File  : web.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date  : 2022/8/25

H
hjdhnx 已提交
7
import socket
H
hjdhnx 已提交
8
from werkzeug.utils import import_string
H
hjdhnx 已提交
9
from netifaces import interfaces, ifaddresses, AF_INET
H
hjdhnx 已提交
10
from flask import request
H
hjdhnx 已提交
11
from utils.log import logger
H
hjdhnx 已提交
12 13 14
MOBILE_UA = 'Mozilla/5.0 (Linux; Android 11; M2007J3SC Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045714 Mobile Safari/537.36'
PC_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
UA = 'Mozilla/5.0'
H
hjdhnx 已提交
15
UC_UA = 'Mozilla/5.0 (Linux; U; Android 9; zh-CN; MI 9 Build/PKQ1.181121.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.5.5.1035 Mobile Safari/537.36'
H
hjdhnx 已提交
16 17 18 19
headers = {
        'Referer': 'https://www.baidu.com',
        'user-agent': UA,
}
H
hjdhnx 已提交
20
from time import time
21
import config as settings
H
hjdhnx 已提交
22

H
hjdhnx 已提交
23
def get_host_ip2(): # 获取局域网ip
H
hjdhnx 已提交
24 25 26 27 28 29 30 31
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # print('8888')
        s.connect(('8.8.8.8', 80))  # 114.114.114.114也是dns地址
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip
H
hjdhnx 已提交
32

H
hjdhnx 已提交
33 34 35 36 37 38
def get_host_ip(): # 获取局域网ip
    ips = []
    for ifaceName in interfaces():
        addresses = ''.join([i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr': ''}])])
        ips.append(addresses)
    real_ips = list(filter(lambda x:x and x!='127.0.0.1',ips))
39
    # logger.info(real_ips)
H
hjdhnx 已提交
40 41 42
    jyw = list(filter(lambda x:str(x).startswith('192.168'),real_ips))
    return real_ips[-1] if len(jyw) < 1 else jyw[0]

H
hjdhnx 已提交
43 44
def getHost(mode=0,port=None):
    port = port or request.environ.get('SERVER_PORT')
H
hjdhnx 已提交
45 46
    # hostname = socket.gethostname()
    # ip = socket.gethostbyname(hostname)
H
hjdhnx 已提交
47 48 49 50
    # ip = request.remote_addr
    # print(ip)
    # mode 为0是本地,1是局域网 2是线上
    if mode == 0:
51
        host = f'http://localhost:{port}'
H
hjdhnx 已提交
52
    elif mode == 1:
53 54
        REAL_IP = get_host_ip()
        ip = REAL_IP
55
        host = f'http://{ip}:{port}'
H
hjdhnx 已提交
56
    else:
57
        host = get_conf(settings).get('PLAY_URL','http://cms.nokia.press')
H
hjdhnx 已提交
58 59 60 61 62 63 64 65 66 67
    return host

def get_conf(obj):
    new_conf = {}
    if isinstance(obj, str):
        config = import_string(obj)
    for key in dir(obj):
        if key.isupper():
            new_conf[key] = getattr(obj, key)
    # print(new_conf)
H
hjdhnx 已提交
68 69 70 71 72 73
    return new_conf

def get_interval(t):
    interval = time() - t
    interval = round(interval*1000,2)
    return interval