Matrix Science Mascot Parser toolkit
 
Loading...
Searching...
No Matches
http_helper_getstring.py

Accessing any server using http(s)

1#!/usr/bin/python
2
16
17import msparser
18import sys
19import getopt
20import ntpath
21import time
22
23# ms_range function has been added to make the range supplied consistent with other languages such as Perl and C#
24def ms_range(start, stop, step=1):
25 i = start
26 while i <= stop:
27 yield i
28 i += step
29
30# Define Global argument variables
31msurl = ''
32msaction = ''
33msmgf = ''
34mshttpusername = ''
35mshttppassword = ''
36msusername = ''
37mspassword = ''
38
39# Define help text
40usage = """
41Usage http_client.py --url=<URL> --action=<command> [options]
42
43 --help Display this message
44 --url Base URL, e.g
45 http://your-server/
46 --action An action for a get command.
47 Appended to the base URL
48 --httpUserName the username for an authenticated web server.
49 --httpPassword the password for an authenticated web server.
50 --username Mascot Server user name
51 May be required if Mascot Security is enabled
52 --password Mascot Server password
53 May be required if Mascot Security is enabled
54"""
55# If no arguments supplied, print help and exit
56if len(sys.argv) < 2 :
57 print(usage)
58 sys.exit(1)
59
60# Ensure arguments are legal. if not, exit
61try:
62 opts, args = getopt.getopt(sys.argv[1:],"",["help","url=","action=","httpUserName=","httpPassword=","username=","password="])
63except getopt.GetoptError:
64 sys.exit(1)
65
66# If argument is --help, display help then exit
67for opt, arg in opts:
68 if opt == '--help':
69 print(usage)
70 sys.exit(2)
71
72# Read arguments and assign each option to appropriate variables
73 elif opt in ("--url"):
74 msurl = arg
75 elif opt in ("--action"):
76 msaction = arg
77 elif opt in ("--httpUserName"):
78 mshttpusername = arg
79 elif opt in ("--httpPassword"):
80 mshttppassword = arg
81 elif opt in ("--username"):
82 msusername = arg
83 elif opt in ("--password"):
84 mspassword = arg
85
86# Common Code for all cases
87
88# Create connection settings
89# Any settings for web server authentication or a proxy server should be set here
90objSettings = msparser.ms_connection_settings()
91objSettings.setUserAgent("PythonScriptTest/1.0 " + objSettings.getUserAgent())
92# Try to auto detect any proxy settings
93objSettings.setProxyServerType(msparser.ms_connection_settings.PROXY_TYPE_AUTO)
94
95if mshttpusername:
96 objSettings.setHttpUsername(mshttpusername)
97
98if mshttppassword:
99 objSettings.setHttpPassword(mshttppassword)
100
101# Connect to the server
102httpHelper = msparser.ms_http_helper(msurl, objSettings)
103if not httpHelper.isValid():
104 showErrorsAndExit(httpHelper)
105
106errCodeString, retString = httpHelper.httpGetString(msaction)
107if errCodeString.isOk():
108 print("httpGetString OK : " + retString)
109else:
110 print("Error: " + errCodeString.getErrorText())
111
112errCodeHeader, retHeader = httpHelper.httpGetHeader(msaction)
113if errCodeHeader.isOk():
114 print("httpGetHeader OK : " + retHeader)
115else:
116 print("Error: " + errCodeHeader.getErrorText())
117
118errCodeBufferOpen = httpHelper.httpBufferedOpen(msaction)
119if errCodeBufferOpen.isOk():
120 print("httpBufferedOpen OK : ")
121 while True:
122 errCodeBufferString, retBufferString = httpHelper.httpBufferedGetString(10000)
123 if len(retBufferString) == 0 or not errCodeBufferString.isOk():
124 break
125 print(retBufferString)
126 httpHelper.httpBufferedClose()
127
128
129def showErrorsAndExit(ms_errs):
130 print("Error: %s" % ms_errs.getLastErrorString())
131 errs = ms_errs.getErrorHandler()
132 for i in ms_range(1, errs.getNumberOfErrors()):
133 print("Error Number: " + str(errs.getErrorNumber(i)) + " (" + str(errs.getErrorRepeats(i + 1)) + " times :")
134 sys.exit(1)
135