http请求及数据库操作工具类
This commit is contained in:
35
source/area/china/util/DbUtil.py
Normal file
35
source/area/china/util/DbUtil.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
"""
|
||||
@description 工具类,数据库
|
||||
@author wendell
|
||||
"""
|
||||
from pymongo import MongoClient
|
||||
|
||||
|
||||
def get_client():
|
||||
"""
|
||||
获取连接客户端
|
||||
:return: 链接客户端
|
||||
"""
|
||||
return MongoClient("mongodb://localhost:27017/", connect=False)
|
||||
|
||||
|
||||
def get_db(db=None):
|
||||
"""
|
||||
获取数据库
|
||||
:param db: 默认链接到python数据库
|
||||
:return:
|
||||
"""
|
||||
if not db:
|
||||
db = 'python'
|
||||
return get_client().get_database(name=db)
|
||||
|
||||
|
||||
def close_client(client):
|
||||
"""
|
||||
关闭客户端连接
|
||||
:param client: 连接客户端
|
||||
:return:
|
||||
"""
|
||||
if client:
|
||||
client.close()
|
||||
49
source/area/china/util/RequestUtil.py
Normal file
49
source/area/china/util/RequestUtil.py
Normal file
@@ -0,0 +1,49 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
"""
|
||||
@description 工具类,requests库二次封装
|
||||
@author wendell
|
||||
"""
|
||||
import requests
|
||||
from requests.exceptions import InvalidURL
|
||||
|
||||
|
||||
def get(url, timeout=None, headers=None, encoding=None):
|
||||
"""
|
||||
发送GET请求
|
||||
:param url: URL
|
||||
:param timeout: 超时时间(秒)
|
||||
:param headers: 头部信息
|
||||
:param encoding: 编码
|
||||
:return:
|
||||
"""
|
||||
# 参数验证,URL不能为空
|
||||
if not url:
|
||||
raise InvalidURL("Invalid URL %r" % url)
|
||||
response = requests.get(url, headers=headers, timeout=timeout)
|
||||
if not response:
|
||||
return None
|
||||
if encoding:
|
||||
response.encoding = encoding
|
||||
if response.status_code == 200:
|
||||
r_text = response.text
|
||||
response.close()
|
||||
return r_text
|
||||
response.close()
|
||||
return None
|
||||
|
||||
|
||||
def prepare_url(url):
|
||||
"""
|
||||
预处理URL连接,规范url
|
||||
www.a.com //www.a.com ==>> http://www.a.com
|
||||
:param url: URL
|
||||
:return: 处理的URL
|
||||
"""
|
||||
if not url:
|
||||
return None
|
||||
res = url
|
||||
if url.startswith('//'):
|
||||
res = 'http:' + url
|
||||
elif not url.startswith('http://') and not url.startswith('https://'):
|
||||
res = 'http://' + url
|
||||
return res
|
||||
0
source/area/china/util/__init__.py
Normal file
0
source/area/china/util/__init__.py
Normal file
Reference in New Issue
Block a user