Send a Push Notification Through Proxy

Takashi Yoshida bio photo By Takashi Yoshida

We use Jenkins to build our software every night. A few months ago, we had some reliability problems with Jenkins and jobs did not always run every night. Since then we fixed the reliability issue but I wanted to see if Jenkins jobs are running every night.

Our Jenkins server sits behind firewall and cannot send mail to an external address. And I am not about to bring back my office laptop home.

So I run a simple script to retrive job status from Jenkins on my workstation but the script needs to send me a notification (so I can see the status on my iPhone/Apple Watch).

My workstation also sits behind firewall and it has no direct Internet access. So this is what I needed to do to have the script send me a notification through proxy. The script uses API from pushover.net to send an notification.

import urllib
import urllib2

if __name__ == "__main__":
  # Use your own username/password and proxy server (and port number)
  # for proxy_auth_url
  proxy_auth_url = "http://username:password@proxy_server:8080"
  proxies = {'http': proxy_auth_url, 'https': proxy_auth_url}

  proxy_handler = urllib2.ProxyHandler(proxies)
  opener = urllib2.build_opener(proxy_handler)

  # content of the message depends on the job status
  message = 'Jenkins job status: Successful'
  payload = urllib.urlencode({'token': 'pushover_app_token',
                              'user': 'pushover_user_key',
                              'message': message, })

  request = urllib2.Request('http://api.pushover.net/1/mesages.json',
                            data = payload,
                            headers = {'Content-type': 'application/x-www-form-urlencoded'})
  response = opener.open(request)