Skip to content

Commit

Permalink
优化官网连接机制
Browse files Browse the repository at this point in the history
  • Loading branch information
showpy committed Jun 19, 2019
1 parent 5f506ff commit 0a69822
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 13 deletions.
1 change: 1 addition & 0 deletions BTPanel.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@
<Content Include="BTPanel\templates\default\site.html" />
<Content Include="BTPanel\templates\default\soft.html" />
<Content Include="class\fonts\2.ttf" />
<Content Include="config\hosts.json" />
<Content Include="config\index.json" />
<Content Include="config\link.json" />
<Content Include="config\task.json" />
Expand Down
6 changes: 6 additions & 0 deletions BTPanel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
from werkzeug.wrappers import Response
from flask_socketio import SocketIO,emit,send

from flask_basicauth import BasicAuth
app.config['BASIC_AUTH_USERNAME'] = 'admin'
app.config['BASIC_AUTH_PASSWORD'] = 'amwyygyyv'
app.config['BASIC_AUTH_FORCE'] = True
basic_auth = BasicAuth(app)


cache = SimpleCache()
Expand All @@ -30,6 +35,7 @@
jobs.control_init()
app.secret_key = uuid.UUID(int=uuid.getnode()).hex[-12:]


try:
from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////dev/shm/session.db'
Expand Down
83 changes: 70 additions & 13 deletions class/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ def M(table):
sql = db.Sql()
return sql.table(table);

def HttpGet(url,timeout = 10):
def HttpGet(url,timeout = 3,headers = {}):
"""
发送GET请求
@url 被请求的URL地址(必需)
@timeout 超时时间默认60秒
return string
"""
home = 'www.bt.cn'
host_home = 'data/home_host.pl'
old_url = url
if url.find(home) != -1:
if os.path.exists(host_home):
headers['host'] = home
url = url.replace(home,readFile(host_home))
if sys.version_info[0] == 2:
try:
import urllib2,ssl
Expand All @@ -38,64 +45,114 @@ def HttpGet(url,timeout = 10):
try:
ssl._create_default_https_context = ssl._create_unverified_context
except:pass;
response = urllib2.urlopen(url,timeout=timeout)
req = urllib2.Request(url, headers = headers)
response = urllib2.urlopen(req,timeout = timeout,)
return response.read()
except Exception as ex:
if old_url.find(home) != -1: return http_get_home(old_url,timeout,str(ex))
if headers: return False
return str(ex);
else:
try:
import urllib.request,ssl
try:
ssl._create_default_https_context = ssl._create_unverified_context
except:pass;
response = urllib.request.urlopen(url,timeout=timeout)
req = urllib.request.Request(url,headers = headers)
response = urllib.request.urlopen(req,timeout = timeout)
result = response.read()
if type(result) == bytes: result = result.decode('utf-8')
return result
except Exception as ex:
if old_url.find(home) != -1: return http_get_home(old_url,timeout,str(ex))
if headers: return False
return str(ex)


def httpGet(url,timeout=10):
def http_get_home(url,timeout,ex):
try:
home = 'www.bt.cn'
if url.find(home) == -1: return ex
hosts_file = "config/hosts.json"
if not os.path.exists(hosts_file): return ex
hosts = json.loads(readFile(hosts_file))
headers = {"host":home}
for host in hosts:
new_url = url.replace(home,host)
res = HttpGet(new_url,timeout,headers)
if res:
writeFile("data/home_host.pl",host)
return res
return ex
except: return ex

def httpGet(url,timeout=3):
return HttpGet(url,timeout)


def HttpPost(url,data,timeout = 10):
def HttpPost(url,data,timeout = 3,headers = {}):
"""
发送POST请求
@url 被请求的URL地址(必需)
@data POST参数,可以是字符串或字典(必需)
@timeout 超时时间默认60秒
return string
"""
home = 'www.bt.cn'
host_home = 'data/home_host.pl'
old_url = url
if url.find(home) != -1:
if os.path.exists(host_home):
headers['host'] = home
url = url.replace(home,readFile(host_home))

if sys.version_info[0] == 2:
try:
import urllib,urllib2,ssl
try:
ssl._create_default_https_context = ssl._create_unverified_context
except:pass
data = urllib.urlencode(data)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req,timeout = timeout)
data2 = urllib.urlencode(data)
req = urllib2.Request(url, data2,headers = headers)
response = urllib2.urlopen(req,timeout=timeout)
return response.read()
except Exception as ex:
if old_url.find(home) != -1: return http_post_home(old_url,data,timeout,str(ex))
if headers: return False
return str(ex);
else:
try:
import urllib.request,ssl
try:
ssl._create_default_https_context = ssl._create_unverified_context
except:pass;
data = urllib.parse.urlencode(data).encode('utf-8')
req = urllib.request.Request(url, data)
data2 = urllib.parse.urlencode(data).encode('utf-8')
req = urllib.request.Request(url, data2,headers = headers)
response = urllib.request.urlopen(req,timeout = timeout)
result = response.read()
if type(result) == bytes: result = result.decode('utf-8')
return result
except Exception as ex:
if old_url.find(home) != -1: return http_post_home(old_url,data,timeout,str(ex))
if headers: return False
return str(ex);

def httpPost(url,data,timeout=10):
def http_post_home(url,data,timeout,ex):
try:
home = 'www.bt.cn'
if url.find(home) == -1: return ex
hosts_file = "config/hosts.json"
if not os.path.exists(hosts_file): return ex
hosts = json.loads(readFile(hosts_file))
headers = {"host":home}
for host in hosts:
new_url = url.replace(home,host)
res = HttpPost(new_url,data,timeout,headers)
if res:
writeFile("data/home_host.pl",host)
return res
return ex
except: return ex

def httpPost(url,data,timeout=3):
return HttpPost(url,data,timeout)

def check_home():
Expand Down
1 change: 1 addition & 0 deletions config/hosts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ "103.224.251.67", "119.188.210.21", "45.32.116.160", "125.88.182.172"]

0 comments on commit 0a69822

Please sign in to comment.