Python – identifying specific exception types

I was trying to work with DocuSign and was getting HTTPError 401 Unauthorized in response to the API calls that I wanted to handle specifically. I found some notes online to specify the Exception Type along with the except clause, e.g. except urllib2.HTTPError as ex. But this certainly didn’t work in my case. So I found a different way to handle such exceptions:

try:
    #-- Make API call to get HTTPError in return
except Exception as ex:
    if isinstance(ex, urllib2.HTTPError) and ex.code == 401: #-- Unauthorized
        #-- Now do you handling Here

DocuSign – Send documents on Behalf Of Other Users

Quite a pain in the neck to implement this feature. DocuSign has published the SOBO implementation on this link, but unfortunately this would only work on the demo site. Reasons explained below, but just trying to get some working sample code in. The following snippet is how the DocuSign link suggests things should work, but it worked only on Demo, not on Production. Look at the comments below the code for production implementation.

from access import *
import sys, json, urllib, urllib2
import base64

account_id = 'a71d1fc7-xxxx-yyyy-zzzz-eeb0e6a2aa60'
docusign_base_url = 'https://demo.docusign.net/restapi/v2' # or 'https://na2.docusign.net/restapi/v2' for production
api_user = 'apiuser@email.com'
api_user_password = 'docuSignPassword'
api_user_access_token = 'xhasfY&AsdGfLy/NhLcWSmS8ug='
integratorKey = "ACCT-54410dda-31c0-0000-1111-5013abcd20fb"

operating_user = 'varun.verma@email.com'
authenticateStr = "" \
                    "" + username + "" \
                    "" + password + "" \
                    "" + integratorKey + "" \
                    ""

# ------------------------------------------------------------------------
# STEP 1 - Get OAuth Token
# ------------------------------------------------------------------------
url = '%s/oauth2/token' % (docusign_base_url)
print 'Get OAuth Token URL:%s' % (url)
body = 'grant_type=password&client_id=%s&username=%s&password=%s&scope=api' % (integratorKey, username, password)
headers = {'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length' : len(body)}
print body
print headers
req = urllib2.Request(url, body, headers=headers)
print req
print req.headers
response = urllib2.urlopen(req)
json_response = response.read()
print json_response

status = response.code
if (status != 200):
    print("Error calling webservice, status is: %s" % status); sys.exit()

access_token = json.loads(json_response)['access_token']
print 'API User Access Token: %s' % access_token

# ------------------------------------------------------------------------
# STEP 2 - Obtain access token for the operating user
# ------------------------------------------------------------------------
authorization = 'bearer %s' % (access_token)
url = '%s/oauth2/token' % (docusign_base_url)
body = 'grant_type=password&client_id=%s&username=%s&password=%s&scope=api' % (integratorKey, operating_user, password)
headers = {'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length' : len(body), 'Authorization' : authorization}
req = urllib2.Request(url, body, headers=headers)
response = urllib2.urlopen(req)
json_response = response.read()
print json_response

status = response.code
if (status != 200):
    print("Error calling webservice, status is: %s" % status); sys.exit()

access_token = json.loads(json_response)['access_token']
print 'Operating User Access Token: %s' % (access_token)

# ------------------------------------------------------------------------
# STEP 3 - Send signature request on behalf of operating user
# ------------------------------------------------------------------------
f = open(document_name, 'rb')
file_contents = f.read()
f.close()

b64_content = base64.b64encode(file_contents)
dict_envelope = {} # Enter your DocuSign envelope here. Skipping for brevity
requestBody =  "\r\n" + \
                "\r\n" + \
                "--myboundary\r\n" + \
                "Content-Type: application/json\r\n" + \
                "Content-Disposition: form-data\r\n" + \
                "\r\n" + \
                json.dumps(dict_envelope)+"\r\n" + \
                "--myboundary--\r\n" + \
                "\r\n"

authorization = 'bearer %s' % (access_token)
headers = { 'Authorization' : authorization,
            'Accept' : 'application/json',
            'Content-Type' : 'multipart/form-data;boundary=myboundary',
            'Content-Length' : len(requestBody)
            }

url = '%s/accounts/%s/envelopes' % (docusign_base_url, account_id)
req = urllib2.Request(url, data=requestBody, headers=headers)
print "Before request"
response = urllib2.urlopen(req)
print "Response"
json_response = response.read()
print json_response

rsync

Had a hard time using rsync today although I had used this so many times in the past. So I decided to take a note of what I missed. Scenario – pushing and syncing files from master to slave server.

Firstly, make sure you had rsync installed on both servers and not just on the master server. That’s the mistake I made by not installing rsync on the slave server.

sudo apt-get install rsync

Next problem I had was that both my servers were setup on AWS and only had secure login via my private key. So in order for rsync to work, I had to use the execute the ssh with the private key in order to rsync files to the other server. Important not here is that the -e (execute statement) must be the last one to be executed

rsync -av --exclude pg_xlog --exclude postgresql.conf /var/lib/postgresql/9.1/main/ --progress -e 'ssh -i /home/admin/.ssh/pgs-aws.pem' root@54.203.2.73:/var/lib/postgresql/9.1/main/

psycopyg2 – Get dictionary like query results

Wondering how life would be much easier if you could get the query resultset to be a dictionary and you could get the value by using a keyword on the dictionary. psycopg2 by default returns resultset as a tuple so you have to use row[0], etc to get the values. In order to use row[‘column_name’] from the result, you’ll have to use the extras module provided in the psycopg2. Initiate the cursor accordingly and you’ll be able to get the results in form of dictionary.

#!/usr/bin/python
import psycopg2
import psycopg2.extras

DB_CONNECTION_STRING = "host=%s dbname=%s user=%s password=%s" % (DB_HOST, DB_DATABASE, DB_USER, DB_PASSWORD)
dbConn = psycopg2.connect(DB_CONNECTION_STRING)
cur = dbConn.cursor(cursor_factory = psycopg2.extras.DictCursor)
qSelect = "SELECT server, data, report_date FROM kw_server_raw WHERE processed = FALSE"
cur.execute(qSelect)
results = cur.fetchall()
for row in results:
	print row['server']

SSH/SFTP to remove server to execute scripts without being redirected to terminal

If you use a bash script to sftp/ssh into a remote terminal, you’ll notice the sftp prompt show up on the screen and the scripts beyond sftp will not be executed. You can enclose the remote server scripts in an EOM statement like this in order to do what you intend to.

#!/bin/bash
#-- Go to the bottle server directory and get the existing files into the tars directory
#---------------------------------------------------------------------------------------
cd /home/server/bottle/
tar -cvf /home/varun.verma/backups/provisioning-tars/bottle_server.tar *
cd /home/varun.verma/backups/provisioning-tars/
sftpgs <<EOM
cd /home/admin/dev_tar
put bottle_server.tar
EOM

sshgs <<EOM
cd /home/admin/dev_tar/
tar -xvf bottle_server.tar
rm bottle_server.tar
exit
EOM