Hello all,

I am trying to automate my builds so that when I commit a branch with a particular url to my svn repository, my python script (which runs twice daily) will pick up on it, do a build, and upload it to a new trac downloads page. Right now I wish to edit a currently existing page to reference the new url for the page representing my latest release. All my "get"s have been working well with ssl using a new opener, but when I go to "post" i get an HTTP Error 400: Bad request.

Here is the offending code:

def postNewTracPage( newTracPgName, tracDownloadsURL, username, password ):
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, 'https://svn.mydomain.com/trac/', username, password)
    authhandler = urllib2.HTTPBasicAuthHandler(passman)
    opener = urllib2.build_opener(authhandler)
    urllib2.install_opener(opener)

    text=urllib2.urlopen(tracDownloadsURL + '?action=edit').read()
    formToken = parseFormToken(text)
    version = parseVersion(text)
    body=parseBody(text)
    values = {'__FORM_TOKEN' : formToken,
          'action' : 'edit',
          'version' : version,
          'text' : body}
    data = urllib.urlencode(values)
    print "data = " + data
    req = urllib2.Request(tracDownloadsURL, data)
    response = urllib2.urlopen(req) #THIS IS WHERE IT FAILS GRR
    #response = urllib2.urlopen(tracDownloadsURL + '?' + data)
    the_page = response.read()
    print the_page
    #go to new tracDownloadsEditURL, edit page w/ template, commit
    #upload data

Here is the offending stack trace:

data = action=edit&text=newTracPage&__FORM_TOKEN=3c6336f7685777275cce750d&version=26
Traceback (most recent call last):
  File "./parse.sh", line 32, in <module>
    postNewTracPage(newTracPgName, tracDownloadsURL, username, password)
  File "/home/jrobison/workspace/CDMTSAutomate/Utils.py", line 145, in postNewTracPage
    response = urllib2.urlopen(req)
  File "/usr/lib64/python2.5/urllib2.py", line 124, in urlopen
    return _opener.open(url, data)
  File "/usr/lib64/python2.5/urllib2.py", line 387, in open
    response = meth(req, response)
  File "/usr/lib64/python2.5/urllib2.py", line 498, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python2.5/urllib2.py", line 419, in error
    result = self._call_chain(*args)
  File "/usr/lib64/python2.5/urllib2.py", line 360, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.5/urllib2.py", line 823, in http_error_401
    url, req, headers)
  File "/usr/lib64/python2.5/urllib2.py", line 801, in http_error_auth_reqed
    return self.retry_http_basic_auth(host, req, realm)
  File "/usr/lib64/python2.5/urllib2.py", line 811, in retry_http_basic_auth
    return self.parent.open(req)
  File "/usr/lib64/python2.5/urllib2.py", line 387, in open
    response = meth(req, response)
  File "/usr/lib64/python2.5/urllib2.py", line 498, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python2.5/urllib2.py", line 425, in error
    return self._call_chain(*args)
  File "/usr/lib64/python2.5/urllib2.py", line 360, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.5/urllib2.py", line 506, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request

Also, I looked at my "edit page" source in trac and stripped it to look at what it would take to get a minimal post working. It came out with this, which needs its version updated. Opening this in firefox (which has my trac credentials already) and clicking "submit" successfully does what I want it to do (post). The stripped page:

<html>
  <body>
    <div id="content" class="wiki">
      <form id="edit" action="https://svn.mydomain.com/trac/myprojectsite/wiki/DownloadsPageTest" method="post">
        <input type="hidden" name="__FORM_TOKEN" value="3c6336f7685777275cce750d" />
        <input type="hidden" name="action" value="edit" />
        <input type="hidden" name="version" value="27" />
        <input name="text" value="== Download Extras ==" />
        <input type="submit" id="save" name="save" value="Submit Changes" /> 
      </form>
    </div>
  </body>
</html>

It appears that something is wrong with my authentication, or maybe the data values. If I "get" url + '?' + data, the page looks how I want it it is just not posted, which leads me to believe it's not the data, but I just don't know. I also read directly querying the database to modify trac pages would be good, though I don't want my build machine to have credentials to the db, and it "should" be doable in python since my little stripped html can do it.

Help would be appreciated. Thanks python gurus!

-Jon

Maybe the question is just how does one securely post to a website? (Especially Trac!)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.