在python中解析网址

时间:2018-09-23 14:34:45

标签: python json parsing url

我想从数据库信息创建HTTP链接。例如ip和端口号,以某种方式我尝试了此操作,但始终无法解析并提示错误,并希望任何人都可以帮助我

 @app.route('/link_test/<string:id>')
 @is_logged_in
 def link_test(id):
     #Create cursor
     cur = mysql.get_db().cursor()
     #get item by itemno
     result = cur.execute("SELECT * FROM testlist WHERE id = %s", [id])
     doc = cur.fetchone()

        )
     doc = {"id": test[0], "rdno": test[1], "ipno": test[2], "port": test[3]}
     cur.close()
     ipno = test['ipno']
     port = test['port']
     url_uptime = 'https://<string:ipno>:<string:port>/stats/uptime/'  

     return render_template('link_test.html', uptime=json.loads(r_uptime))

2 个答案:

答案 0 :(得分:1)

<string:ipno>:<string:port>是Flask路径,不是从字面上用作URL的东西

如果您想创建此字符串,则称为格式化,而不是解析

ipno = doc['ipno']
port = doc['port']
url_uptime = 'https://{}:{}/stats/uptime/'.format(ipno, port)
r_uptime = requests.get(url_uptime, verify=False).content

您还需要在创建test之前在某处定义doc

答案 1 :(得分:0)

getVars1 = doc [2]

url ='/ stats / uptime /' url_uptime =('https://'+ getVars1 + url)

非常感谢同伴