简体   繁体   English

bottle Web服务器:如何路由到文本文件

[英]bottle web server: how to route to text file

I am writing a web app using bottle. 我正在使用bottle编写Web应用程序。 I need to pass certain text files to a javascript function which process the file and displays an image on the web. 我需要将某些文本文件传递给javascript函数,该函数处理该文件并在网络上显示图像。

I would like to be able to make a route with a variable directory ie 我希望能够使用可变目录(即

./database/*/CONTCAR.xyz ./database/*/CONTCAR.xyz

so that i can call a url of the form ./database/6Ni@32Ag_npo/CONTCAR.xyz and get that CONTCAR.xyz returned, where "6Ni@32Ag_npo" will be different for each URL. 这样我就可以调用形式为./database/6Ni@32Ag_npo/CONTCAR.xyz的URL,并获得返回的CONTCAR.xyz,其中,每个URL的“ 6Ni @ 32Ag_npo”都不同。

Here is what I have in my server 这是我服务器中的东西

import bottle as b
@b.route('/database/<folder>/CONTCAR.xyz')
  def server_static(filename):
     return b.static_file( "CONTCAR.xyz" , root='./database/<folder>')

In my javascript I try to to call the url as follows: 在我的JavaScript中,我尝试按以下方式调用网址:

<canvas class='xyz' url='/database/6Ni@32Ag_npo/CONTCAR.xyz' filetype='xyz'></canvas>

the xyz class is a class that allows me to process this CONTCAR file. xyz类是允许我处理此CONTCAR文件的类。

I get the following error: 我收到以下错误:

TypeError: server_static() got an unexpected keyword argument 'folder' localhost - - [19/Jan/2014 13:10:46] "GET /database/6Ni@32Ag_npo/CONTCAR.xyz?uid=1390158646852 HTTP/1.1" 500 794 TypeError:server_static()得到了意外的关键字参数“文件夹”本地主机--[19 / Jan / 2014 13:10:46]“ GET /database/6Ni@32Ag_npo/CONTCAR.xyz?uid=1390158646852 HTTP / 1.1” 500 794

You're using the name folder in the route's path, but filename as the sole parameter name. 您使用的是路径路径中的名称folder ,但filename是唯一的参数名称。 Luckily, the fix is easy: just use the same name in both places. 幸运的是,修复很容易:只需在两个地方使用相同的名称。 (And also correct your use of folder in static_file 's root param: (并且还要更正您在static_fileroot参数中对folder的使用:

@b.route('/database/<folder>/CONTCAR.xyz')
  def server_static(folder):
     return b.static_file('CONTCAR.xyz', root='./database/{}'.format(folder))

For two levels of folders, you'd do something like this: 对于两个级别的文件夹,您将执行以下操作:

@b.route('/database/<folder1>/<folder2>/CONTCAR.xyz')
  def server_static(folder1, folder2):
     return b.static_file('CONTCAR.xyz', root='./database/{}/{}'.format(folder1, folder2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM