1

我对 Flask 完全陌生。我有一些代码可以使用 pysphere 库将文件复制到虚拟机。这本身就可以正常工作,但是当我尝试使用 Flask 应用程序时,我得到:

UnicodeDecodeError: 'ascii' codec can't decode byte 0x90 in position 2: ordinal not in range(128)

起初我认为这只是因为网络表单传递了它不喜欢的东西。但是,我决定对这些值进行硬编码,但它仍然失败。这是代码:

@app.route('/begin_install', methods=['POST'])
def begin_install():
    source_installer_path = app.root_path + '/installers'
    installer_file = str(request.form['installer'])
    option_file_path = app.root_path + '/installers/options'
    option_file = 'testing.options'

    vmserver.start_install( request.form['vm'],
                        source_installer_path,
                        installer_file,
                        option_file_path,
                        option_file)

    return render_template('results.html')

然后,在我的 pysphere 相关文件中:

def start_install(self, vmpath, installer_path, installer_file, options_path, options_file):
    vm.revert_to_named_snapshot('python_install')
    vm.power_on()
    while vm.get_tools_status() != 'RUNNING':
        sleep(3)
    vm.login_in_guest(self.guest_user, self.guest_password)
    vm.send_file('C:\\folder\\filetosend.exe', 'c:\\installer\\filename.exe')

直到“vm.send_file”的所有内容都运行良好。如果我从非 Flask 应用程序调用相同的代码,它也可以正常工作。当这部分代码都是 pysphere 时,为什么我会从 Flask 收到错误,我对此感到非常困惑。

编辑:这是回溯

Traceback (most recent call last):
  File "C:\Users\username\Flask\lib\site-packages\flask\app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\username\Flask\lib\site-packages\flask\app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Users\username\Flask\lib\site-packages\flask\app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\username\Flask\lib\site-packages\flask\app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\username\Flask\lib\site-packages\flask\app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\username\Flask\lib\site-packages\flask\app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\python\PycharmProjects\installtest\installtest.py", line 51, in begin_install
    option_file)
  File "C:\python\PycharmProjects\installtest\testmachines.py", line 56, in start_install
    'c:\\installer\\filename.exe')
  File "C:\Users\username\Flask\lib\site-packages\pysphere\vi_virtual_machine.py", line 1282, in send_file
    resp = opener.open(request)
  File "C:\Python27\Lib\urllib2.py", line 400, in open
    response = self._open(req, data)
  File "C:\Python27\Lib\urllib2.py", line 418, in _open
    '_open', req)
  File "C:\Python27\Lib\urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "C:\Python27\Lib\urllib2.py", line 1215, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "C:\Python27\Lib\urllib2.py", line 1174, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "C:\Python27\Lib\httplib.py", line 958, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\Lib\httplib.py", line 992, in _send_request
    self.endheaders(body)
  File "C:\Python27\Lib\httplib.py", line 954, in endheaders
    self._send_output(message_body)
  File "C:\Python27\Lib\httplib.py", line 812, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0x90 in position 2: ordinal not in range(128)
4

1 回答 1

0

正如您在回溯pysphere使用urllib2中看到的那样,反过来使用httplib.

据我所知,httplib 正在连接整个 http 请求,而无需进一步检查 unicode 字符串。如果一部分是 unicode,python 总是返回 unicode。

我认为您的pysphere在方法中request或方法中会以某种方式被 unicode污染:openersend_file

  文件“C:\Users\username\Flask\lib\site-packages\pysphere\vi_virtual_machine.py”,第 1282 行,在 send_file
    resp = opener.open(请求)

你应该检查你是如何创建resp的。配置您的vm实例,并可能详细说明哪个标头是 unicode 以及它如何影响requestopener实例化。

于 2013-01-04T13:33:30.720 回答