forked from mdruger/python-faxage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
72 lines (59 loc) · 2.77 KB
/
example.py
File metadata and controls
72 lines (59 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os, os.path, optparse, httplib, urllib, base64
HOST = "www.faxage.com"
def send_fax(document, username, company, password, recip_fax, recip_name='', sender_name='', sender_phone=''):
fax_data = file(document, 'r').read()
fax_data_b64 = base64.b64encode(fax_data)
document_name = os.path.basename(document)
parameters = {
#API call and authentication:
'host': HOST,
'username': username,
'company': company,
'password': password,
'operation': 'sendfax',
#fax information:
'faxfilenames[]': document_name,
'faxfiledata[]': fax_data_b64,
#recipient information:
'faxno': recip_fax,
'recipname': recip_name,
#sender information:
'tagname': sender_name,
'tagnumber': sender_phone,
}
payload_length = 0
payload = []
for key, value in parameters.items():
item = '%s=%s' % (key, urllib.quote(value))
payload.append(item)
payload_length += len(item)
# account for & between items.
payload_length += len(payload) - 1
conn = httplib.HTTPSConnection(HOST)
conn.putrequest("POST", '/httpsfax.php')
conn.connect()
conn.putheader('Content-Type', 'application/x-www-form-urlencoded')
conn.putheader('Content-Length', payload_length)
conn.endheaders()
conn.send('&'.join(payload))
resp = conn.getresponse()
print 'Response...'
print resp.read()
if __name__ == '__main__':
parser = optparse.OptionParser(prog="example", description="Send a fax via FAXAGE using python.")
parser.add_option("-a", "--document", help="The document to fax.")
parser.add_option("-u", "--username", help="The FAXAGE API username.")
parser.add_option("-c", "--company", help="The FAXAGE API company.")
parser.add_option("-p", "--password", help="The FAXAGE API password.")
parser.add_option("-r", "--recipient", help="The fax number to send the document to.")
parser.add_option("-d", "--debug", action="store_true", help="Break into debugger before the interesting bits.")
(options, args) = parser.parse_args()
if not options.document:
parser.error('You must supply a document to fax using the --document argument.')
if not options.username or not options.company or not options.password:
parser.error('You must supply a username, company and password to access the FAXAGE API.')
if not options.recipient:
parser.error('You must supply a fax number using the --recipient argument.')
if options.debug:
import pdb; pdb.set_trace()
send_fax(options.document, options.username, options.company, options.password, options.recipient)