简体   繁体   English

链接到静态Tornado页面中的外部URL

[英]Link to external url in static Tornado page

I've made a html file and a python script to control my raspberry pi GPIO pins via websocket with the Tornado Web Server. 我制作了一个html文件和一个python脚本,以使用Tornado Web服务器通过websocket控制我的树莓派GPIO引脚。 It works great. 效果很好。 To serve my static html page I use the following code in the python script: 为了提供我的静态html页面,我在python脚本中使用以下代码:

application = tornado.web.Application([
    (r'/escape/media/(.*)',tornado.web.StaticFileHandler,{"path":'/home/pi/myproject/media'}),
    (r'/escape/(.*)',tornado.web.StaticFileHandler,{"path":'/home/pi/myproject'}),
    (r'/mycode',WSHandler)
])
if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    print 'Websocket Server Start ...'
    tornado.ioloop.IOLoop.instance().start()

In the html file I try to load an external mp3 file to play: 在html文件中,我尝试加载外部mp3文件以播放:

function talk() {
            text = encodeURI(document.getElementById("textinput").value);
            mp3 = new Audio('http://translate.google.com/translate_tts?tl=en&q=' + text);
            mp3.play();
        }

This works if I load the webpage from my own disk on my windows machine but not when Tornado servers it as a static page from the raspberry pi. 如果我从Windows计算机上自己的磁盘加载网页,但在Tornado将其作为树莓派的静态页面进行服务器处理时,则无法运行。 In the javascript console I get the message: "Failed to load resource: the server responded with a status of 404 (Not Found)". 在javascript控制台中,我收到消息:“无法加载资源:服务器响应状态为404(未找到)”。

I don't understand why this is a problem because it's an absolute link to an external website. 我不明白为什么这是一个问题,因为它是到外部网站的绝对链接。 Does anyone know the solution? 有人知道解决方案吗?

Thanks! 谢谢!

It looks like the problem is that Google is blocking the use of this API based on the Referer header. 看来问题在于Google禁止基于Referer标头使用此API。 When the file is on local disk no Referer is sent and it works, but when the file is accessed over HTTP the Referer is sent and Google returns a 404. See Request to Google Text-To-Speech API for more details including possible workarounds. 当文件位于本地磁盘上时,不会发送引荐来源,并且可以正常工作,但是当通过HTTP访问引荐来源时,Google会返回404。有关更多详细信息(包括可能的解决方法),请参阅向Google Text-To-Speech API的请求

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

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