python - 如何使用 Python 替换 Get 请求中的用户输入值
问题描述
网址在单个变量中定义,并将根据用户输入用于获取请求,请在下面找到代码...
d = {'Mango': 'http://12.345.67.891:8000/api/datasources/proxy/1/query?db=UK_GHS&q=SELECT%20sum(%22count%22)%20FROM%20%22gatling%22%20WHERE%20%22status%22%20%3D%20%27ok%27%20AND%20%22simulation%22%20%3D~%20%2Fsoak-test*%2F%20AND%20time%20%3E%201544491800000ms%20and%20time%20%3C%201544495400000ms%20GROUP%20BY%20%22script%22&epoch=ms',
'Banana':'http://12.345.67.891:8000/api/datasources/proxy/1/query?db=UK_GHS&q=SELECT%20sum(%22count%22)%20FROM%20%22gatling%22%20WHERE%20%22status%22%20%3D%20%27ok%27%20AND%20%22simulation%22%20%3D~%20%2Fspike-test*%2F%20AND%20time%20%3E%201544491800000ms%20and%20time%20%3C%201544495400000ms%20GROUP%20BY%20%22script%22&epoch=ms',
'Apple':'http://12.345.67.891:8000/api/datasources/proxy/1/query?db=UK_GHS&q=SELECT%20sum(%22count%22)%20FROM%20%22gatling%22%20WHERE%20%22status%22%20%3D%20%27ok%27%20AND%20%22simulation%22%20%3D~%20%2Fload-test*%2F%20AND%20time%20%3E%201544491800000ms%20and%20time%20%3C%201544495400000ms%20GROUP%20BY%20%22script%22&epoch=ms'}
Dashboard_name = raw_input("Enter dashboard name from the above list :")
如果用户输入 Mango 获取请求将采用第一个 URL,如果用户输入香蕉获取请求将采用第二个 URL,依此类推
user_input_from = raw_input("Enter from date and time in YYYY-MM-DD HH:MM:SS format :")
user_input_to = raw_input("Enter to date and time in YYYY-MM-DD HH:MM:SS format :")
def user_input_to_epoch_time(user_input):
return int(time.mktime(time.strptime(user_input, '%Y-%m-%d %H:%M:%S')))
final_url = d.format(user_input_to_epoch_time(user_input_from),
user_input_to_epoch_time(user_input_to))
print(final_url)
results = d.get(Dashboard_name, 'No value in list')
print results
我实现了您建议的代码,但是由于 URL 不是直接的,并且它根据用户输入而出现以下错误。
final_url = d.format(user_input_to_epoch_time(user_input_from),
AttributeError: 'dict' object has no attribute 'format'
解决方案
下面的代码:
import time
SHORT_URL_TEMPLATE_MANGO = 'http://12.345.67.891:8000/api/mango?from={}&to={}'
SHORT_URL_TEMPLATE_APPLE = 'http://12.345.67.891:8000/api/apple?from={}&to={}'
URLS = {'mango': SHORT_URL_TEMPLATE_MANGO, 'apple':
SHORT_URL_TEMPLATE_APPLE}
def user_input_to_epoch_time(user_input):
return int(time.mktime(time.strptime(user_input, '%Y-%m-%d %H:%M:%S')))
dash_board = 'mango'
user_input_from = "2018-10-12 23:12:44"
user_input_to = "2018-10-12 23:17:55"
final_url = URLS[dash_board].format(user_input_to_epoch_time(user_input_from),
user_input_to_epoch_time(user_input_to))
print(final_url)
输出:
http://12.345.67.891:8000/api/mango?from=1539375164&to=1539375475
推荐阅读
- python - 根据多个不同的值创建新列
- python - 计算相关矩阵
- flutter - Google Places AutoComplete 搜索时不显示地址
- xaml - 如何知道 ItemsRepeater 视口上的索引
- mysql - MySQL - GROUP BY 和 HAVING 来确定支出
- android - 在真实设备上不调用广播接收器
- c# - 读取连接字符串参数
- r - 从 R 中的不平衡贸易流数据创建邻接矩阵
- javascript - 我需要从这个数组中获取所有可能的 10 种组合到另一个数组中。我从我的代码中得到的只是 3 种组合的可能
- linux - 如何使用 sed 转义所有特殊字符