pysphere
pysphere copied to clipboard
vm.send_file: <unicode> URL + <str> file contents = UnicodeDecodeError
What steps will reproduce the problem?
1. Set up your server connection
2. Get a VM entity
3. Call `vm.send_file(bla, bla)`
What is the expected output? What do you see instead?
I expect the local file to be uploaded to the guest. Instead, I've been seeing
stuff like this:
File "/usr/lib/python2.7/site-packages/pysphere/vi_virtual_machine.py", line 1193, in send_file
resp = opener.open(request)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1215, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "/usr/lib/python2.7/httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 992, in _send_request
self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 812, in _send_output
msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 0: ordinal
not in range(128)
What version of the product are you using? On what operating system?
I'm the latest svn revision, or r75 of `vi_virtual_machine.py`. Only testing
with Python 2.7 on Arch Linux.
Please provide any additional information below.
This problem only started appearing recently--I've been able to send files to
guest systems without a problem since I started using pysphere a month or two
ago. I'm not sure what changed, exactly, but somewhere along the line it
appears that the "msg" you see in the last line of the stack trace is a unicode
object while the "message_body" from the same line is a str object. The "msg"
variable holds a bunch of header information, while the "message_body" contains
the actual binary content of the file I'm trying to send.
I've watched/debugged this off and on for a few days, and it seems that the
problem goes away if I simply force the URL to be a str object sometime before
this line in vi_virtual_machine.py:
request = urllib2.Request(url, data=content)
Original issue reported on code.google.com by [email protected]
on 30 Jul 2012 at 5:02
Thanks for the detailed info. I'll be checking that out
Original comment by [email protected]
on 31 Jul 2012 at 12:58
I reported and showed how to fix this issue as a one line change in the google
group:
https://groups.google.com/forum/#!searchin/pysphere/michael$20specter/pysphere/A
7EPrquJAnQ/q3EJhNw-UIAJ
TL;DR
On line 1280 in vi_virtual_machine.py, change
request = urllib2.Request(url, data=content)
To:
request = urllib2.Request(url.encode('utf-8'), data=content)
-Mike
Original comment by [email protected]
on 8 Aug 2013 at 4:53
Any chance in getting this fixed and releasing a new version to Pypi?
Tim
Original comment by [email protected]
on 8 Aug 2013 at 4:54
I've been rolling my own build with this exact patch since before I created
this issue. It has been working great for me.
Original comment by [email protected]
on 8 Aug 2013 at 4:57
More than 2 years later.... any chance this could become official yet? I've
been using my original patch this whole time, and I've not had any bad side
effects from it.
Original comment by [email protected]
on 6 Oct 2014 at 8:07