Skip to content

Instantly share code, notes, and snippets.

@catermelon
Last active January 28, 2016 07:05
Show Gist options
  • Save catermelon/b70d79ea670f048ed165 to your computer and use it in GitHub Desktop.
Save catermelon/b70d79ea670f048ed165 to your computer and use it in GitHub Desktop.
Script to easily test XML requests against an exchange server
#!/usr/bin/env python
import os
import logging
import requests
from requests_ntlm import HttpNtlmAuth
import getpass
import xml.dom.minidom
logging.basicConfig(level=logging.DEBUG)
EXCHANGE_SERVER = ''
DOMAIN = 'DOMAIN'
USERNAME = os.environ.get('EXCHANGE_USERNAME') or 'DUMMY_USERNAME'
# PASSWORD is prompted for
# This just asks the server for all the timezones it knows about
# http://msdn.microsoft.com/en-us/library/dd899430(v=exchg.140).aspx
REQUEST = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<soap:Header>
<t:RequestServerVersion Version="Exchange2010"/>
</soap:Header>
<soap:Body>
<m:GetServerTimeZones ReturnFullTimeZoneData="false">
</m:GetServerTimeZones>
</soap:Body>
</soap:Envelope>
"""
if __name__ == '__main__':
if EXCHANGE_SERVER == '' or USERNAME == "DUMMY_USERNAME" or DOMAIN == "DOMAIN":
raise SystemExit("Hey, you need to edit the script to add your custom information before you run it.")
PASSWORD = os.environ.get('EXCHANGE_PASSWORD') or getpass.getpass()
HEADERS = {
'Content-type': 'text/xml; charset=utf-8 ',
'Accept': 'text/xml'
}
response = requests.post(EXCHANGE_SERVER,
auth=HttpNtlmAuth('%s\\%s' % (DOMAIN, USERNAME), PASSWORD),
data=REQUEST,
headers=HEADERS)
# lxml is better, but all I want to do is pretty print the XML response
print(xml.dom.minidom.parseString(response.text).toprettyxml())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment