[python-bugzilla] How to attach file ?

Cole Robinson crobinso at redhat.com
Fri Jan 17 15:07:08 UTC 2014


On 01/17/2014 04:54 AM, Khokhar cth wrote:
> Hi Cole,
> 
> I think I've figured out the reason why attached file being corrupted.
> To upload the file we need to pass Base64 encoded data.
> Data has to encode as Base64 and data must be declared as Base64 encoded as
> well. Else it's passed as a string and is not decoded by Bugzilla.
> I presume the following code of "attachfile" method needs to be modify in base.py
> 
>         data = f.read()
>         if not isinstance(data, bytes):
>             data = data.encode(locale.getpreferredencoding())
>         kwargs['data'] = Binary(data)
> 
> I've tried like this but unfortunately it doesn't work.
>      
> #    data = f.read() 
> #    encoded_data = base64.b64encode(data)
> #    kwargs['data'] = encoded_data
> 
> Reference Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=958559
> 
> Any Suggestion?
> 

If I add this patch to git:

diff --git a/bugzilla/base.py b/bugzilla/base.py
index bb7b940..0a6213b 100644
--- a/bugzilla/base.py
+++ b/bugzilla/base.py
@@ -34,6 +34,9 @@ import requests
 from bugzilla import __version__, log
 from bugzilla.bug import _Bug, _User

+import httplib
+httplib.HTTPConnection.debuglevel = 1
+

 # Backwards compatibility
 Bug = _Bug



Then run an attach command like:

PYTHONPATH=. ./bugzilla-cli --debug --bugzilla partner-bugzilla.redhat.com
attach --file ~/py.png 946419

I see this result:

send: "POST /xmlrpc.cgi HTTP/1.1\r\nHost:
partner-bugzilla.redhat.com\r\nContent-Length: 1557\r\nAccept-Encoding: gzip,
deflate, compress\r\nAccept: */*\r\nUser-Agent: Python/Bugzilla\r\nCookie:
Bugzilla_logincookie=Jzkuaso0fH; Bugzilla_login=199727\r\nContent-Type:
text/xml\r\n\r\n<?xml
version='1.0'?>\n<methodCall>\n<methodName>Bug.add_attachment</methodName>\n<params>\n<param>\n<value><struct>\n<member>\n<name>file_name</name>\n<value><string>py.png</string></value>\n</member>\n<member>\n<name>data</name>\n<value><base64>\niVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A\n/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9gEGxE4IQYzJ14AAAI3SURBVDjL\nZZNPSFVBFIe/e9+zd3silBCl0SZoU4s2rVq0EB5tQip4UNvATVGu3QRBiyAi2iltWkgbF5EgRhFF\nRpiWtrWIzDIV1Pzz7p15M2fmtvDevOqBw8DM9zvnN8ycgF3R/eDtM2mac96ZdrFNxBikqbRV+vHH\n/ut9gAZczoe7C3gnF0f6au1OLM5avFi8d1Ea+JvAMSAq8nsKOGs5f2cYJ3Y7rc2PO4BqkS8DdD98\nf9tbe1ysCoxOBo1qlEXHJWcM4b5KPU19zleA0o4Clx99eO3EdqVewHsCoFRugUoVghJO7A6H6Vx9\nwdtYi27cr5x6dy/03nVtWTU7bWeZh6jNUcAiCaFTURl9A+gs56AviHzh3mnqtdPxm6knfQPLU7Ua\nokASQq/agY7yDrG16Mba6Pz48NP56VdrgAApYObGaicPtkovToFLQBKA/WUxTe3FRk4san15aGKg\nd3Dj560rrdGJS6FT0X9YYvLuiMKL1kAQOpHZ3PqfyZfP41+9PW1VfzX0RXFSECfgNEmSTgImdDru\nF2O0E8vvqZG1auQubAsKooIYYHpGvwA2g+xndQBHgWa6!
 cG0ih5cW/w
6VvEq3nChwCoBvs+bL2Z7V\nceBHGTDAIrABpMVuhw+4OiLgLIglOLPYBTQAlfErIeCzjRVg1dtEb1kt5Omv+DTV2YssAN+zNdkz\nC42N9brV8WdvYp07seOdM2Of1F3AAknW0AJpwN6IgEPAEaANaMlcbmZdl7KRBuAfAb+v//yMAJoA\nAAAASUVORK5CYII=\n</base64></value>\n</member>\n<member>\n<name>ids</name>\n<value><array><data>\n<value><string>946419</string></value>\n</data></array></value>\n</member>\n<member>\n<name>content_type</name>\n<value><string>application/octet-stream</string></value>\n</member>\n<member>\n<name>summary</name>\n<value><string>py.png</string></value>\n</member>\n</struct></value>\n</param>\n</params>\n</methodCall>\n"
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Fri, 17 Jan 2014 15:04:14 GMT
header: Server: Apache
header: SOAPServer: SOAP::Lite/Perl/0.710.06
header: X-Content-Type-Options: nosniff
header: X-Frame-Options: SAMEORIGIN
header: X-Xss-Protection: 1; mode=block
header: Vary: Accept-Encoding,User-Agent
header: Content-Encoding: gzip
header: Content-Length: 164
header: Content-Type: text/xml
header: Connection: close
Created attachment 728575 on bug 946419


In the 'send' bit, you can see that the data is declared as <base64>, so it
seems like we are doing the correct thing here.

What does that change give you?

- Cole




More information about the python-bugzilla mailing list