- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 Python + Flask 开发网络应用程序。从最简单的意义上讲,客户端对特定 URL 的请求将触发应用程序登录到服务器端的远程机器,执行一系列 shell 命令,并将输出(JSON 格式)解析并发送给客户端作为响应的一部分。这些命令相当简单。要登录到远程机器,我唯一可用的方法是 rlogin,所以我使用了 pexpect 模块,因为我 couldn't find rlogin 的任何标准 Python 模块。
现在的问题是,虽然我能够从 pexpect/rlogin 获得正确的输出,但将该输出(字符串)作为响应发送会导致错误:
----------------------------------------
Exception happened during processing of request from ('171.71.55.54', 62736)
Traceback (most recent call last):
File "/isan/python/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/isan/python/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/isan/python/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/isan/python/python2.7/SocketServer.py", line 651, in __init__
self.finish()
File "/isan/python/python2.7/SocketServer.py", line 710, in finish
self.wfile.close()
File "/isan/python/python2.7/socket.py", line 279, in close
self.flush()
File "/isan/python/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 9] Bad file descriptor
----------------------------------------
我已将代码精简到重现错误所需的最低限度。对此应用程序的 HTTP 请求导致“文件描述符错误”错误。抱歉缩进不同,我使用了两个不同的编辑器来修改代码!
import os
import subprocess
import pexpect
from flask import Flask
app = Flask(__name__)
class rlogin:
def __init__(self, host, prompt):
self.child = pexpect.spawn("rlogin " + host)
self.prompt = prompt
self.child.expect_exact(self.prompt)
def command(self, command):
self.child.sendline(command)
self.child.expect_exact(self.prompt)
response = self.child.before
return response
def close(self):
self.child.close()
@app.route('/')
def index():
rl = rlogin("myserver", "root@myserver:~# ")
output = rl.command("pwd")
rl.close()
# The output of the next line is just as I expect:
print output
# This is probably where it fails:
return output
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
删除对 rlogin()
、rl.command()
和 rl.close()
的调用,并返回一个简单的字符串,例如“A”解决了错误。我已经在这个问题上停留了一段时间,非常感谢任何帮助。谢谢!
最佳答案
您必须确保返回类型是 str、unicode、响应类或 WSGI 函数。看起来您的输出不属于任何可接受的 Flask 路由返回类型。
@app.route('/')
def index():
rl = rlogin("myserver", "...")
output = rl.command("pwd")
rl.close()
print type(output)
# Convert output into something that flask can understand
value = str(output)
resp = make_response(value)
resp.headers['Content-Type'] = 'text/plain'
return resp
您可以在 http://flask.pocoo.org/docs/0.10/api/#flask.Flask.make_response 阅读更多相关信息
关于Python + Flask 网络应用报告 "[Errno 9] Bad file descriptor"与 pexpect 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31306725/
我们的电脑在使用的过程中,有的小伙伴在上网的时候可能就遇到过系统提示:400 bad request的情况。据小编所知这种情况,大致意思就是出现了错误的请求或者请求不能满足。原因是因为我们请求的语法
您可以尝试清除浏览器缓存 访问一下你的FTP看是否可以登陆 成功解决502 Bad Gateway错误 今天登陆博客,显示502 bad gateway,NGINX最烦人的地方就是经常会出现这个
我想要具有 FIFO 的服务器-客户端模型和客户端获取目录路径,但我收到错误“读:错误地址”和“写:错误地址”。 客户端 服务器错误:“读取:地址错误” 客户端错误:“写入:地址错误” 最佳答案 您可
Agda 手册 Inductive Data Types and Pattern Matching状态: To ensure normalisation, inductive occurrences
我正在使用 maven-compiler-plugin:2.3.2 并且每次我对在导入中具有枚举 (ContentType) 的类进行更改时,我需要使 干净,否则它会给我: ERROR] Failed
我想发布我的第一个 Facebook 应用程序,需要一个隐私政策 URL。 我在我的网站上发布了 privacypolicy.html 页面,但是当我在“应用程序详细信息”中配置它时,我收到了下一条消
vscode 1.45.1版本使用克隆存储库时,我收到“Bad credentials”。最近我在github上换了用户名。可能就是这个原因。我如何告诉vs code?
我正在 Mac OS 终端上创建 cron,代码如下: home.cron 的内容: * * * * * /users/username/desktop/forTrump/script.sh 然后我这
我是新手,所以需要任何帮助,当我要求一个例子时,我的教授给我了这段代码,我希望有一个工作模型...... from numpy import loadtxt import numpy as np fr
我使用 linux 服务器已经有一段时间了,通过使用 cifs 挂载到多个 Windows 共享。 到目前为止,我总是在/etc/fstab 中有一行://IPADDRESS/sharename/mn
请大家帮帮我我正在尝试使用 NUTCH 抓取网站,但它给我错误“java.io.IOException: Job failed!” 我正在运行此命令“bin/nutch solrindex http:
我想创建我的基础业务类,例如 EntityBase,以具有一些常见的行为,例如实现用于跟踪对象更改的接口(interface)(IsNew、IsDirty)和 INotifyPropertyChang
我们最近开发了一个基于 SOA 的站点,但是这个站点在负载过重时最终会出现严重的负载和性能问题。我在这里发布了一个与此问题相关的问题: ASP.NET website becomes unrespon
我们的 Azure 功能已开始返回 502 Bad Gateways,但并非所有调用都返回。我没有使用“间歇性”这个词,因为它总是进行相同类型的调用,但现在总是使用相同的数据。 常规配置 Azure
我假设在字典中进行查找时,它需要散列您提供的 key ,然后使用该散列来查找您要查找的对象。 如果是这样,使用较大的对象作为键是否会显着减慢查找速度或产生其他使用字符串或简单数据类型作为键不会遇到的后
我的代码如下: public static final Condition.ActionCondition ACTION_CONDITION_ACTIVATE = new Condit
大家好,我有一个应用程序和一个表单,我要求用户在其中输入地址,并在文本字段下方显示带有标记的谷歌地图,用户可以在其中将标记拖/放到正确的位置。问题是,在显示 map 的开始时,它只是部分显示而不是全部
给定字节矩阵(所有值在内存中都是 1 位),如果其中至少有一个零,则称其为原始列或“坏”列。查找算法,占用 O(1) 额外内存。 如果没有另一个值(如 -1)或另一个重复矩阵来跟踪已经找到的空值,并且
当我创建一个标准类时,我主要这样做: $test = null; $test->id = 1; $test->name = 'name'; 但是在严格模式下我得到一个错误。 显然正确的做法是: $te
我试图理解为什么将 -O2 -march=native 与 GCC 一起使用会比不使用它们时产生更慢的代码。请注意,我在 Windows 7 下使用 MinGW (GCC 4.7.1)。 这是我的代码
我是一名优秀的程序员,十分优秀!