在python 2.6中,以下代码:
import urlparse
qsdata = "test=test&test2=test2&test2=test3"
qs = urlparse.parse_qs(qsdata)
print qs
Run Code Online (Sandbox Code Playgroud)
给出以下输出:
{'test': ['test'], 'test2': ['test2', 'test3']}
Run Code Online (Sandbox Code Playgroud)
这意味着即使测试只有一个值,它仍然被解析为一个列表.有没有办法确保如果只有一个值,它不会被解析成一个列表,所以结果看起来像这样?
{'test': 'test', 'test2': ['test2', 'test3']}
Run Code Online (Sandbox Code Playgroud) 我有一个用 FastAPI 编写的简单路线,如下所示,
from fastapi import FastAPI
app = FastAPI()
@app.get("/foo/bar/{rand_int}/foo-bar/")
async def main(rand_int: int):
return {"path": f"https://some-domain.com/foo/bar/{rand_int}/foo-bar/?somethig=foo"}
Run Code Online (Sandbox Code Playgroud)
我怎样才能“以编程方式”获取当前路径,
some-domain.com)/foo/bar/{rand_int}/foo-bar/)?somethig=foo)我有一个 FastAPI 应用程序,我希望将默认日志写入 STDOUT,并使用 JSON 格式的以下数据:
应用程序日志应如下所示:
{
"XYZ": {
"log": {
"level": "info",
"type": "app",
"timestamp": "2022-01-16T08:30:08.181Z",
"file": "api/predictor/predict.py",
"line": 34,
"threadId": 435454,
"message": "API Server started on port 8080 (development)"
}
}
}
Run Code Online (Sandbox Code Playgroud)
访问日志应如下所示:
{
"XYZ": {
"log": {
"level": "info",
"type": "access",
"timestamp": "2022-01-16T08:30:08.181Z",
"message": "GET /app/health 200 6ms"
},
"req": {
"url": "/app/health",
"headers": {
"host": "localhost:8080",
"user-agent": "curl/7.68.0",
"accept": "*/*"
},
"method": "GET",
"httpVersion": "1.1",
"originalUrl": "/app/health",
"query": {}
},
"res": {
"statusCode": 200,
"body": …Run Code Online (Sandbox Code Playgroud) 我正在编写一个 Fast API 服务器,它接受请求,检查用户是否获得授权,然后如果成功则将他们重定向到另一个 url。
我需要携带 URL 参数,例如
http://localhost:80/data/?param1=val1¶m2=val2 应该重定向到 http://some.other.api/?param1=val1¶m2=val2,从而保留之前分配的参数。
有些参数不是我控制的,随时可能改变。
我怎样才能做到这一点?
代码:
from fastapi import FastAPI
from starlette.responses import RedirectResponse
app = FastAPI()
@app.get("/data/")
async def api_data():
params = '' # I need this value
url = f'http://some.other.api/{params}'
response = RedirectResponse(url=url)
return response
Run Code Online (Sandbox Code Playgroud) 此代码适用于 Flask
import requests
from flask import Flask,request,session,render_template, jsonify
import requests,time
from datetime import datetime,timedelta
app = Flask(__name__,template_folder='./')
@app.route('/')
def home():
return render_template("For_putting_lat_lon.html")
@app.route('/distance')
def check():
Latitude1 = request.args.get("Latitude1")
Latitude2 = request.args.get("Latitude2")
Longitude1 = request.args.get("Longitude1")
Longitude2 = request.args.get("Longitude2")
data = list()
result = Travel_distance(Latitude1, Longitude1, Latitude2, Longitude2,data)
if(result == 0):
return render_template("noavailable.html")
return render_template("For_posting_lat_lon.html",data = data)
def Travel_distance(Latitude1, Longitude1, Latitude2, Longitude2, data):
geolocator = Nominatim(user_agent="geoapiExercises")
location_name1 = geolocator.geocode(str(Latitude1)+", " +str(Longitude1))
location_name2 = geolocator.geocode(str(Latitude2)+", "+str(Longitude2))
req = requests.get(f"""http://router.project-osrm.org/route/v1/drive/{Longitude1},{Latitude1};{Longitude2},{Latitude2}?overview=false""") #car,bike,foot
route_1 = …Run Code Online (Sandbox Code Playgroud) 如果这是一个重复的问题,我很抱歉,但除了这个网站之外,我几乎在任何地方都看不到它。这个网站最初是帮助人们通过 REST API 在 Google Sheet 中进行查询。他们的链接就像这个例子:
spreadsheetID/sheetName?filter[ColumnName]=aiman
Run Code Online (Sandbox Code Playgroud)
我只是想知道如何在 FastAPI 中执行此操作,特别是对于 params filter[columnName]。columnName 也是一个根据列名称的变量。我已经读过这篇文章但无法很好地理解它。我也欣赏任何语言。