python-3.x - 如何解决从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 ' 返回的文件保存在一个文件夹中。
解决方案
您的浏览器尝试显示/呈现此 xml 内容。这样做可能会导致一些错误。试试这个,看看它是否适合你:
import html
@get("/getjourney")
def getjourney():
response = "Journey1"
f = open('XML files/' + response + '.xml').read()
return html.escape(f)
推荐阅读
- javascript - 无法从 selenium webdriver 的 Kendo 下拉菜单中选择一个选项
- compiler-errors - F# Implicit ModuleSuffix not working when using fully qualified name
- python - 在python中,如何减去两列具有时间值的excel文件并创建它的新列?
- swift - 在 Core Data 中检索具有过滤子级的父级
- python - 多线程使我的代码有一些问题
- azure-devops - 如何在发布管道中使用来自构建管道的自定义变量
- python - 如何使用包含 12 个值的平均每月数据的线性插值来生成精确中点的 365 天数据?
- android - 在清除搜索视图中的数据时,它应该删除过滤列表并填充原始列表
- python - 类型错误:+ 不支持的操作数类型:使用 str(sum(list)) 时的“int”和“str”
- android - 如何在 adb run-as 命令中使用“if”?