Jenkins: BadHTTPException: Error communicating with server

python-jenkins

Posted by Dongyupu on November 23, 2017

There appears to be a minor bug in the get_version() function.

If the Jenkins server is setup with security enabled and IF the anonymous user doesn’t have the ‘Overall Read’ rights then you need to pass the authorization key to urlopen()

i.e. in the file ‘jenkins/init.py’, the get_version() function needs to be changed from:

def get_version(self):
    try:
        request = Request(self._build_url(''))
        request.add_header('X-Jenkins', '0.0')
        response = urlopen(request, timeout=self.timeout)

To:

def get_version(self):
    try:
        request = Request(self._build_url(''))
        if self.auth:
            request.add_header('Authorization', self.auth)
        request.add_header('X-Jenkins', '0.0')
        response = urlopen(request, timeout=self.timeout)

To find where this module is on your system, run python and enter:

import jenkins
jenkins.__file__

(I have reported the bug to the module authors)