调用pysphere的Flask app中的UnicodeDecodeError

时间:2012-12-21 21:33:25

标签: python flask

我对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)

1 个答案:

答案 0 :(得分:0)

正如您在追溯中所看到的那样pysphere使用urllib2,而httplib依次使用request

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

我认为你的 pysphere在openersend_file方法的vm中使用unicodes会以某种方式解决

  File "C:\Users\username\Flask\lib\site-packages\pysphere\vi_virtual_machine.py", line 1282, in send_file
    resp = opener.open(request)

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