首页 > 解决方案 > 如何解决从url读取文件时瓶子路由不断加载的错误?

问题描述

我需要保存由 url ' http://127.0.0.1:8080/getjourney ' 发送的文件。当我通过 python 文件代码运行程序时。但是,当通过功能浏览器使用相同的代码路由功能时,浏览器卡在加载和服务器崩溃中。你能帮我解决这个问题吗?

import urllib
from urllib.request import urlopen
from bottle import run, route, template, request, get, post
from urllib.parse import urlparse


@get("/getjourney")
def getjourney():
    response = "Journey1"
    f = open('XML files/' + response + '.xml').read()
    return "f"


@route('/savejourney')
def savejourney():
    url = 'http://127.0.0.1:8080/getjourney'
    response = urllib.request.urlopen(url)
    xml1 = response.read()
    print(xml1)
    xml = open('../XML files/new.xml', "w")
    xml.write(str(xml1))

我希望将 url ' http://127.0.0.1:8080/getjourney ' 返回的文件保存在一个文件夹中。

标签: python-3.xbottle

解决方案


您的浏览器尝试显示/呈现此 xml 内容。这样做可能会导致一些错误。试试这个,看看它是否适合你:

import html
@get("/getjourney")
def getjourney():
    response = "Journey1"
    f = open('XML files/' + response + '.xml').read()
    return html.escape(f)

推荐阅读