web.py 2.3 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
def get_host_ip2(): # 获取局域网ip
H
hjdhnx 已提交
21 22 23 24 25 26 27 28
    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 已提交
29

H
hjdhnx 已提交
30 31 32 33 34 35 36 37 38 39 40 41
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))
    logger.info(real_ips)
    jyw = list(filter(lambda x:str(x).startswith('192.168'),real_ips))
    return real_ips[-1] if len(jyw) < 1 else jyw[0]

REAL_IP = get_host_ip()

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