在拥抱服务器中像字符串一样传递路径 [英] Passing path like string in hug server
问题描述
有没有办法在拥抱中传递带有斜线的字符串,例如,使用以下功能:
Is there any way to pass a string with slashes in hug, for example with this function:
import hug
@hug.get("/returnfilecontent/{path}")
def doubles(path):
return open(path, 'r').read()
我要访问 http:// localhost / returnfilecontent / foo / bar / myfile .md
从位于 foo / bar / myfile.md
的文件中读取内容。
I want to access to http://localhost/returnfilecontent/foo/bar/myfile.md
to read contents from file located in foo/bar/myfile.md
.
似乎拥抱不是使用路径很好,并且我只能传递非路径字符串,例如 http://localhost/returnfilecontent/myfile.md
It seems hug does not behave well with paths and I can only pass non-path strings like http://localhost/returnfilecontent/myfile.md
推荐答案
我不确定这是否是您要寻找的,可能会有所帮助
I'm not sure whether this is what you are looking for, May be this helps
import hug
@hug.get("/returnfilecontent")
def doubles(request, path: hug.types.text):
return open(path, 'r').read()
您可以通过以下方式调用此获取请求
You can call this get request by
curl http://localhost:8000/returnfilecontent/\?path\=foo\/bar\/myfile.md
或者您可以尝试将其作为foo_bar_myfile.md传递并拆分并加入使其成为路径
Or you can try passing in those as foo_bar_myfile.md and split and join it to make it a path
或像这样
import hug
@hug.get("/returnfilecontent/{base_path}/{middle_folder}/{filename}")
def doubles(request, base_path: hug.types.text, middle_folder, filename):
return f"{base_path}/{middle_folder}/{filename}"
这篇关于在拥抱服务器中像字符串一样传递路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!