[Product-Developers] zope.testbrowser: browser.open with content-type gives error

Kees Hink keeshink at gmail.com
Mon May 14 14:20:10 UTC 2012


Looking at the zope.testbrowser doc [1], it seems i can send an XML file 
in this manner: browser.post(url, data, content_type)

I've tried this in a script [2], but get "TypeError: must be string or 
buffer, not dict" [3]. With python 2.6 it's no different.

Am i missing something?

Kees

[1] 
http://pypi.python.org/pypi/zope.testbrowser#submitting-a-posts-body-directly


[2] upload_script.py
# Run from a virtualenv that has zope.testbrowser installed.

import argparse

from zope.testbrowser.browser import Browser

parser = argparse.ArgumentParser(description='Upload a K4-XML-export 
document to a running server')
parser.add_argument('filename', help='The XML file to send')
args = parser.parse_args()

def main():
     browser = Browser()
     import_url = 'http://localhost:8080/website/@@k4import'
     xmlfile = open(args.filename)
     xmldata = xmlfile.read()
     browser.post(import_url, xmldata, 'application/xml')

if __name__ == '__main__':
     main()


[3] stacktrace:
(zope.testbrowser)kees at francis:~/development/zope/src/freitag.k4import/freitag/k4import/k4$ 
python upload_script.py dtd_valid.xml
Traceback (most recent call last):
   File "upload_script.py", line 19, in <module>
     main()
   File "upload_script.py", line 16, in main
     browser.post(import_url, xmldata, 'application/xml')
   File 
"/home/kees/.virtualenvs/zope.testbrowser/local/lib/python2.7/site-packages/zope.testbrowser-4.0.2-py2.7.egg/zope/testbrowser/browser.py", 
line 277, in post
     return self.open(url, data)
   File 
"/home/kees/.virtualenvs/zope.testbrowser/local/lib/python2.7/site-packages/zope.testbrowser-4.0.2-py2.7.egg/zope/testbrowser/browser.py", 
line 253, in open
     self.mech_browser.open(url, data)
   File 
"/home/kees/.virtualenvs/zope.testbrowser/local/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.py", 
line 203, in open
     return self._mech_open(url, data, timeout=timeout)
   File 
"/home/kees/.virtualenvs/zope.testbrowser/local/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.py", 
line 230, in _mech_open
     response = UserAgentBase.open(self, request, data)
   File 
"/home/kees/.virtualenvs/zope.testbrowser/local/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_opener.py", 
line 193, in open
     response = urlopen(self, req, data)
   File 
"/home/kees/.virtualenvs/zope.testbrowser/local/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_urllib2_fork.py", 
line 344, in _open
     '_open', req)
   File 
"/home/kees/.virtualenvs/zope.testbrowser/local/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_urllib2_fork.py", 
line 332, in _call_chain
     result = func(*args)
   File 
"/home/kees/.virtualenvs/zope.testbrowser/local/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_urllib2_fork.py", 
line 1142, in http_open
     return self.do_open(httplib.HTTPConnection, req)
   File 
"/home/kees/.virtualenvs/zope.testbrowser/local/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg/mechanize/_urllib2_fork.py", 
line 1115, 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 818, in _send_output
     self.send(message_body)
   File "/usr/lib/python2.7/httplib.py", line 790, in send
     self.sock.sendall(data)
   File "/usr/lib/python2.7/socket.py", line 224, in meth
     return getattr(self._sock,name)(*args)
TypeError: must be string or buffer, not dict



More information about the Product-Developers mailing list