HTTP

Posting to Slack

The Python httplib and related modules work but make life harder than it needs to be. Thankfully, the requests module acts as a wrapper and makes issuing HTTP requests as simple as you would expect. Installation of the module is with pip as you would expect and there is a decent introductory guide on the site.

I had to revisit the requests module in order to post to Slack. I wanted my orchestration server to keep everyone updated when deployments happened and this seemed like a good solution. A few steps to install the Incoming WebHooks integration and everything was ready.

The integration helpfully displays a curl command for posting to any channel which can be replicated in Python with only minor changes

import requests
# see your integration config for webhook URL
webhookurl = 'https://hooks.slack.com/services/...'
payload    = { 'text': 'This is a test post' }
response   = requests.post(webhookurl,json=payload)
print("%d - %s" % (response.status_code,response.reason))

To change the channel to post to, the displayed user/bot name or the icon just add the necessary keys to the payload dictionary. The example provided by Slack provides all three.

At present I just want to push messages to the channel but for those feeling more adventurous you can try building a full chatbot. There are a couple of Python modules already to start you off but I’ve not had chance to try any of them yet.

Proxies

Accessing information via a URL is easily done using urllib but it is not always able to automatically detect proxy settings. Manually setting a proxy is not that difficult, it even handles authentication.

<br /># Python 3 use urllib.request<br />import urllib<br />proxy = { 'http': 'http://proxy.server:port' }<br />conn = urllib.FancyURLopener(proxy)<br />with conn.open("http://www.bbc.co.uk/") as page:<br /><%%KEEPWHITESPACE%%>    print "Header\n",page.info(),"\nPage\n",page.read()<br />

The proxy passed is a dictionary where the key is the protocol and value is the proxy. Different protocols can have different proxies although this is unlikely in practice.

Once you have connection, you can use the open command and use it a file. If the with statement seems unusual, see this post for an explanation.

Python 3 users note that data returned from the various read methods will return binary data just the same as a file read would. You can convert this to a (unicode) string with calling str.decode()

If you are only using HTTP and you need more control over what is being sent / received you can use httplib. The added power creates added complexity but to add a proxy pass it to the connection method as shown

<br />import httplib<br />conn = httplib.HTTPConnection("proxy.server:port") # or "proxy.server",port<br />conn.request("GET","http://www.bbc.co.uk/")<br />resp = conn.getresponse()<br />print resp.status<br />conn.close()<br />