42 lines
1013 B
Python
42 lines
1013 B
Python
from pymongo import MongoClient
|
|
|
|
# 获取数据集
|
|
def col():
|
|
# 链接数据库
|
|
conn = MongoClient('mongodb://curl:jV9cL0eW6mX4@mongo:27017/curl')
|
|
return conn.curl['curl']
|
|
|
|
# 通过尾缀获取源地址
|
|
def getUrlByTail(tail):
|
|
try:
|
|
url = col().find_one({'tail': tail}, {"_id": 0, "url": 1})
|
|
if url:
|
|
return (url, 200)
|
|
else:
|
|
return ('URL不存在', 100)
|
|
except Exception as e:
|
|
print(e)
|
|
return ('数据库查询失败', 101)
|
|
|
|
# 通过源地址获取尾缀
|
|
def getTailByUrl(url):
|
|
try:
|
|
tail = col().find_one({'url': url}, {"_id": 0, "tail": 1})
|
|
if tail:
|
|
return (tail, 200)
|
|
else:
|
|
return ('尾缀不存在', 102)
|
|
except Exception as e:
|
|
print(e)
|
|
return ('数据库查询失败', 103)
|
|
|
|
# 新增记录
|
|
def insertTail(data):
|
|
try:
|
|
col().insert_one(data)
|
|
return ('OK', 200)
|
|
except Exception as e:
|
|
print(e)
|
|
return ('数据库插入失败', 104)
|
|
|
|
# time 时间戳 url 源地址 tail 生成的尾缀 |