requests
module, and parsing the xml response using xml.etree.cElementTree
. Then a quick inspection of the objects created follows.The following links were used to help get these things working.
In [2]:
%run ../../code/version_check.py
Date Created: 2017-03-25
Date of Change Change Notes
-------------- ----------------------------------------------------------------
2017-03-25 Initial draft
2017-04-02 Added "file saved: <location>" output
In [3]:
%run ../../code/eoddata.py
from getpass import getpass
import json
import os
import os.path
import requests as r
import stat
import xml.etree.cElementTree as etree
ws = 'http://ws.eoddata.com/data.asmx'
ns='http://ws.eoddata.com/Data'
session = r.Session()
In [4]:
username = getpass()
In [5]:
password = getpass()
Create credentials file for later usage. The file will have permissions created so only the current user can access the file. The following SO post was followed.
The following directory will be created if it doesn't exist:
%USERPROFILE%/.eoddata
~/.eoddata
In [6]:
# gather credentials
credentials = {'username': username, 'password': password}
# set filename variables
credentials_dir = os.path.join(os.path.expanduser("~"), '.eoddata')
credentials_file_name = 'credentials'
credentials_path = os.path.join(credentials_dir, credentials_file_name)
# set security variables
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL # Refer to "man 2 open".
mode = stat.S_IRUSR | stat.S_IWUSR # This is 0o600 in octal and 384 in decimal.
# create directory for file if not exists
if not os.path.exists(credentials_dir):
os.makedirs(credentials_dir)
# for security, remove file with potentially elevated mode
try:
os.remove(credentials_path)
except OSError:
pass
# open file descriptor
umask_original = os.umask(0)
try:
fdesc = os.open(credentials_path, flags, mode)
finally:
os.umask(umask_original)
# save credentials in secure file
with os.fdopen(fdesc, 'w') as f:
json.dump(credentials, f)
f.write("\n")
print("file saved: {}".format(credentials_path))
In [7]:
call = 'Login'
url = '/'.join((ws, call))
payload = {'Username': username, 'Password': password}
response = session.get(url, params=payload, stream=True)
if response.status_code == 200:
root = etree.parse(response.raw).getroot()
In [8]:
dir(root)
Out[8]:
In [9]:
for child in root.getchildren():
print (child.tag, child.attribute)
In [10]:
for item in root.items():
print (item)
In [11]:
for key in root.keys():
print (key)
In [11]:
print (root.get('Message'))
print (root.get('Token'))
print (root.get('DataFormat'))
print (root.get('Header'))
print (root.get('Suffix'))
In [12]:
token = root.get('Token')
In [13]:
# client can be opened using a with statement
with (Client()) as eoddata:
print('token: {}'.format(eoddata.get_token()))
In [14]:
# initialise using secure credentials file
eoddata = Client()
# client field accessors
ws = eoddata.get_web_service()
ns = eoddata.get_namespace()
token = eoddata.get_token()
session = eoddata.get_session()
print('ws: {}'.format(ws))
print('ns: {}'.format(ns))
print('token: {}'.format(token))
print(session)
In [15]:
# the client has a list of exchange codes avaiable once intialised
eoddata.get_exchange_codes()
Out[15]:
In [16]:
# client must be closed if opened outside a with block
session.close()
eoddata.close_session()