上要求:
上代码:
客户端:
"""前端的作用,
---------------------------------------------------------------------------------------
1.功能1:收集长链接并生成短链接
dag流程:收集长URL,并确认长URL有效,如果有效则生成短URL,并传递到后端服务器。
1-1 收集长URL: 通过在index.html 中设计一个表单,客户输入表单后,用requests.form 方法获取表单内容
1-2 检查长URL是否有效:通过checkurl(longurl)函数,当longurl 有效则返回1,否则返回0
1-3 生成短URL:通过createshorturl(longurl)函数,从长url里面截取字段另外加上时间来生成一个短url
1-4 传递到后端服务器:把长短URL的关系记录到后端
具体实现可以通过requests.post 方法,向对应的restful api 进行请求(备注1),传递jason字段(备注2)
备注1:这里指post 的url,指requsts.post 请求对应的Url,建议post的url要详细到具体的接口,比如127.0.0.1:8008/api/shorturlcreate 接口,这样后端服务器可以根据接口特性来进行相应的逻辑处理
备注2:制作字典,按照 dic1= {短链:{"长链":长链,"访问日期":"null","创建日期":date}} 创建
另外,requests.post 方式,选择data=json 方式传递,json字段是对备注2里字典信息做字符串序列化的结果 json.dumps(dic1)
2.功能2:收集短链接做相关跳转
dag流程:收集短url,如果短url存在,则接收服务端的redirect请求,跳转到短URL对应的长URL链接上。
2-1 收集短url:通过在index.html设计一个表单,客户输入表单后,用requests.form方法获取表单内容
2-2 检查短url是否存在:通过checksurl(shorturl)函数,当shorturl有效则返回1,否则返回0 (服务端)
2-3 跳转到长URL:通过jumpurl(shorturl)函数,请求到服务端。服务端先判断短URL是否存在,如果存在则做redirect。
jumpurl函数设计:
1.访问到后端服务器,requests.post(url/api/jump,""), 后端服务器根据URL具体的api来返回信息,返回值里包含了URL信息
2.服务端要构造返回的Header值,根据Location 参数 ,让客户浏览器跳转到对应的长链URL中
3.功能3:请求某个短链的访问信息,并进行展示:
dag流程:收集短url 对应的信息,如果短URL存在,则返回这个短URL被调度的次数等信息
3-1 在第2-3步,每次服务端指定后端做跳转的时候,把短URL被访问的时间做一个记录。
服务端操作:
每次访问短链的时候,对访问情况做个统计:
3-1-1: {短url:访问时间} 存入redis 另外一个表里。
3-2-2:对之前的 数据结构做个更新,主要是对 shorturl 的value做个update,访问时间增加一行。
3-2 发起请求,通过requests.post() 函数,传参数为 /api/statics ,到后端
收集后端返回的json字符串,查询时间,短url,以及这个url的访问列表,{date:[list],"shorturl",url, visittime:[list]}。
用json.loads() 把信息拿到,然后收集visittime的信息,
按照天的维度对visittime做一个图像化。---作业。
"""
from flask import Flask,render_template,request,url_for,redirect
from datetime import date
import requests
import json
import time
def check_if_valid(longurl):
#检查长链接是否有效,这里mock一下
result = 1
return result
def check_exist_url(longurl):
#检查长链接是否在服务端存在,mock一下
result = 0
return result
def post(url,notes):
#模拟postman,传递数据到服务端。
print("deliver notes to serverside")
return 1
def create_shorturl(longurl):
#返回shorturl
return "/baidu"
def check_shorturl_if_exist(shorturl):
return 0
app = Flask(__name__)
@app.route('/',methods=['GET','POST'])
def index():
if request.method == 'POST': #根据post 表单获取的内容做相关判断
longurl = request.form['longurl']
shorturl = request.form['shorturl']
print("longurl is {0}, shorturl is {1}".format(longurl,shorturl))
if longurl is not None: #当longurl 非空
if check_if_valid(longurl) == 1 and check_exist_url(longurl) == 0: #当longurl 可用,且longurl在服务端不存在
shorturl = create_shorturl(longurl)
notes = {"shorturl": shorturl, "longurl": longurl}
url = 'http://127.0.0.1:8008/api/shorturlcreate'
post(url, notes)
else: #否则条件没达到,通知失败
notes = "the longurl is not exist or it's already at serverside"
return render_template('display.html', info=notes)
if shorturl is not None: #当shorturl 非空,执行第二个逻辑
if check_shorturl_if_exist(shorturl) == 1:# 如果短url在服务端存在,则做跳转等逻辑
url = 'http://127.0.0.1:8008/api/shorturljump'
post(url, notes)
print("jump success")
return render_template('display.html',info="jump success")
else:
notes = "the shorturl is not exist"
return render_template('display.html', info=notes)
else:
return render_template('index2.html')
@app.route('/api/static',methods=['GET','POST'])
def get_static():
print("The site info is displayed")
return render_template('display.html', info="The site info is displayed")
if __name__ == '__main__':
app.run(host='0.0.0.0',port=80,debug = True)
服务端:
"""前端的作用,
---------------------------------------------------------------------------------------
1.功能1:收集长链接并生成短链接
dag流程:收集长URL,并确认长URL有效,如果有效则生成短URL,并传递到后端服务器。
1-1 收集长URL: 通过在index.html 中设计一个表单,客户输入表单后,用requests.form 方法获取表单内容
1-2 检查长URL是否有效:通过checkurl(longurl)函数,当longurl 有效则返回1,否则返回0
1-3 生成短URL:通过createshorturl(longurl)函数,从长url里面截取字段另外加上时间来生成一个短url
1-4 传递到后端服务器:把长短URL的关系记录到后端
具体实现可以通过requests.post 方法,向对应的restful api 进行请求(备注1),传递jason字段(备注2)
备注1:这里指post 的url,指requsts.post 请求对应的Url,建议post的url要详细到具体的接口,比如127.0.0.1:8008/api/shorturlcreate 接口,这样后端服务器可以根据接口特性来进行相应的逻辑处理
备注2:制作字典,按照 dic1= {短链:{"长链":长链,"访问日期":"null","创建日期":date}} 创建
另外,requests.post 方式,选择data=json 方式传递,json字段是对备注2里字典信息做字符串序列化的结果 json.dumps(dic1)
2.功能2:收集短链接做相关跳转
dag流程:收集短url,如果短url存在,则接收服务端的redirect请求,跳转到短URL对应的长URL链接上。
2-1 收集短url:通过在index.html设计一个表单,客户输入表单后,用requests.form方法获取表单内容
2-2 检查短url是否存在:通过checksurl(shorturl)函数,当shorturl有效则返回1,否则返回0 (服务端)
2-3 跳转到长URL:通过jumpurl(shorturl)函数,请求到服务端。服务端先判断短URL是否存在,如果存在则做redirect。
jumpurl函数设计:
1.访问到后端服务器,requests.post(url/api/jump,""), 后端服务器根据URL具体的api来返回信息,返回值里包含了URL信息
2.服务端要构造返回的Header值,根据Location 参数 ,让客户浏览器跳转到对应的长链URL中
3.功能3:请求某个短链的访问信息,并进行展示:
dag流程:收集短url 对应的信息,如果短URL存在,则返回这个短URL被调度的次数等信息
3-1 在第2-3步,每次服务端指定后端做跳转的时候,把短URL被访问的时间做一个记录。
服务端操作:
每次访问短链的时候,对访问情况做个统计:
3-1-1: {短url:访问时间} 存入redis 另外一个表里。
3-2-2:对之前的 数据结构做个更新,主要是对 shorturl 的value做个update,访问时间增加一行。
3-2 发起请求,通过requests.post() 函数,传参数为 /api/statics ,到后端
收集后端返回的json字符串,查询时间,短url,以及这个url的访问列表,{date:[list],"shorturl",url, visittime:[list]}。
用json.loads() 把信息拿到,然后收集visittime的信息,
按照天的维度对visittime做一个图像化。---作业。
"""
from flask import Flask,render_template,request,url_for,redirect
from datetime import date
import requests
import json
import time
def check_if_valid(longurl):
#检查长链接是否有效,这里mock一下
print("----come to function--- check_if_valid(longurl)")
try:
r = requests.get("https://"+longurl)
statuscode = r.status_code
if statuscode == 200 or statuscode == 301:
print("The site is reachable on internet")
result = 1
else:
result = 0
except:
result = 0
return result
def check_if_exist_url(longurl):
print("----come to function--- check_if_exist_url(longurl)")
#检查长链接是否在服务端存在,mock一下
print("Process: check if the longurl {0} is exist at serverside redis storage".format(longurl))
r = requests.post("http://127.0.0.1:8008/api/checkurl", json={"longurl": longurl})
textjson = r.json()
print("get the return info from the serverside {0}".format(longurl))
print(textjson)
print("get the type info of the serverside")
print(type(textjson))
print("Get the longurl ")
print(textjson["longurl"])
#print(dic2["longurl"])
result = textjson["result"]
return result
def post_shorturlcreate(shorturl,longurl):
#模拟postman,传递数据到服务端。
print("----come to function--- post_shorturlcreate(shorturl,longurl)")
print("Process: to deliver create link to serverside redis storage")
r = requests.post("http://127.0.0.1:8008/api/shorturlcreate",json={"shorturl":shorturl,"longurl":longurl})
print("get info from serverside
"+ r.text)
return 1
def post_shorturljump(shorturl):
print("----come to function--- post_shorturljump(shorturl)")
print("Process: jump to shorturl")
r = requests.post("http://127.0.0.1:8008/api/shorturljump", json={"shorturl": shorturl})
print("get info from serverside
" + r.text)
return r
def create_shorturl(longurl):
print("----come to function--- create_shorturl(longurl)")
print("Process: to create shorturl from longurl")
#返回shorturl
shorturl = "/"+longurl.split(".")[1]
print("Process:The short url is "+shorturl)
return shorturl
def check_shorturl_if_exist(shorturl):
print("----come to function--- check_shorturl_if_exist()")
print("Process: check if the shorturl {0} is exist at serverside redis storage".format(shorturl))
r = requests.post("http://127.0.0.1:8008/api/checkshorturl", json={"shorturl": shorturl})
textjson = r.json()
print("Print the info return from serverside,this is the info")
print(textjson)
print("Chcek the type of the info")
print(type(textjson))
print("Check the mapping(longurl) of the shorturl {0}".format(shorturl))
print(textjson["shorturl"])
# print(dic2["longurl"])
result = textjson["result"]
return result
app = Flask(__name__)
@app.route('/',methods=['GET','POST'])
def index():
if request.method == 'POST': #根据post 表单获取的内容做相关判断
longurl = request.form['longurl']
shorturl = request.form['shorturl']
print("longurl is {0}, shorturl is {1}".format(longurl,shorturl))
if longurl is not None and shorturl == "empty": #当longurl 非空
if check_if_valid(longurl) == 1 and check_if_exist_url(longurl) == 0: #当longurl 可用,且longurl在服务端不存在
shorturl = create_shorturl(longurl)
post_shorturlcreate(shorturl, longurl)
else: #否则条件没达到,通知失败
notes = "the longurl is not exist or it's already at serverside"
return render_template('display.html', info=notes)
if shorturl is not None and longurl == "empty": #当shorturl 非空,执行第二个逻辑
print("进入第二个逻辑")
shorturl ='/' + shorturl
if check_shorturl_if_exist(shorturl) == 1:# 如果短url在服务端存在,则做跳转等逻辑
r = post_shorturljump(shorturl)
print(r.json())
print(type(r.json()))
longurl = r.json()["longurl"]
print(longurl)
return redirect("https://" + longurl)
else:
notes = "the shorturl is not exist"
return render_template('display.html', info=notes)
else:
return render_template('index5.html')
@app.route('/api/static',methods=['GET','POST'])
def get_static():
print("The site info is displayed")
return render_template('display.html', info="The site info is displayed")
if __name__ == '__main__':
app.run(host='0.0.0.0',port=80,debug = True)
index5.html
57-54
display.html
57-53
{{info}}
前端
键入www.qq.com
点击记录
实现了插入逻辑:
插入逻辑前端日志:
插入逻辑后端日志:
查看数据库:
跳转前逻辑:
键入qq,点击跳转
跳转后:
前端日志:
后端日志:
看访问做了跳转302,
update后,跳转时间做了记录
页面更新:2024-03-19
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号