Accessing any server using http(s)
#!/usr/bin/python ############################################################################## # file: http_helper_getstring.py # # 'msparser' toolkit example code # ############################################################################## # COPYRIGHT NOTICE # # Copyright 1998-2010 Matrix Science Limited All Rights Reserved. # # # ############################################################################## # $Source: parser/examples/test_python/http_helper_getstring.py $ # # $Author: villek@matrixscience.com $ # # $Date: 2018-07-30 16:23:53 +0100 $ # # $Revision: 1b450440f9c97e1e41d0fc6016a27d68951d4532 | MSPARSER_REL_2_8_1-0-gea32989045 $ # # $NoKeywords:: $ # ############################################################################## import msparser import sys import getopt import ntpath import time # ms_range function has been added to make the range supplied consistent with other languages such as Perl and C# def ms_range(start, stop, step=1): i = start while i <= stop: yield i i += step # Define Global argument variables msurl = '' msaction = '' msmgf = '' mshttpusername = '' mshttppassword = '' msusername = '' mspassword = '' # Define help text usage = """ Usage http_client.py --url=<URL> --action=<command> [options] --help Display this message --url Base URL, e.g http://your-server/ --action An action for a get command. Appended to the base URL --httpUserName the username for an authenticated web server. --httpPassword the password for an authenticated web server. --username Mascot Server user name May be required if Mascot Security is enabled --password Mascot Server password May be required if Mascot Security is enabled """ # If no arguments supplied, print help and exit if len(sys.argv) < 2 : print(usage) sys.exit(1) # Ensure arguments are legal. if not, exit try: opts, args = getopt.getopt(sys.argv[1:],"",["help","url=","action=","httpUserName=","httpPassword=","username=","password="]) except getopt.GetoptError: sys.exit(1) # If argument is --help, display help then exit for opt, arg in opts: if opt == '--help': print(usage) sys.exit(2) # Read arguments and assign each option to appropriate variables elif opt in ("--url"): msurl = arg elif opt in ("--action"): msaction = arg elif opt in ("--httpUserName"): mshttpusername = arg elif opt in ("--httpPassword"): mshttppassword = arg elif opt in ("--username"): msusername = arg elif opt in ("--password"): mspassword = arg # Common Code for all cases # Create connection settings # Any settings for web server authentication or a proxy server should be set here objSettings = msparser.ms_connection_settings() objSettings.setUserAgent("PythonScriptTest/1.0 " + objSettings.getUserAgent()) # Try to auto detect any proxy settings objSettings.setProxyServerType(msparser.ms_connection_settings.PROXY_TYPE_AUTO) if mshttpusername: objSettings.setHttpUsername(mshttpusername) if mshttppassword: objSettings.setHttpPassword(mshttppassword) # Connect to the server httpHelper = msparser.ms_http_helper(msurl, objSettings) if not httpHelper.isValid(): showErrorsAndExit(httpHelper) errCodeString, retString = httpHelper.httpGetString(msaction) if errCodeString.isOk(): print("httpGetString OK : " + retString) else: print("Error: " + errCodeString.getErrorText()) errCodeHeader, retHeader = httpHelper.httpGetHeader(msaction) if errCodeHeader.isOk(): print("httpGetHeader OK : " + retHeader) else: print("Error: " + errCodeHeader.getErrorText()) errCodeBufferOpen = httpHelper.httpBufferedOpen(msaction) if errCodeBufferOpen.isOk(): print("httpBufferedOpen OK : ") while True: errCodeBufferString, retBufferString = httpHelper.httpBufferedGetString(10000) if len(retBufferString) == 0 or not errCodeBufferString.isOk(): break print(retBufferString) httpHelper.httpBufferedClose() def showErrorsAndExit(ms_errs): print("Error: %s" % ms_errs.getLastErrorString()) errs = ms_errs.getErrorHandler() for i in ms_range(1, errs.getNumberOfErrors()): print("Error Number: " + str(errs.getErrorNumber(i)) + " (" + str(errs.getErrorRepeats(i + 1)) + " times :") sys.exit(1)
Copyright © 2022 Matrix Science Ltd. All Rights Reserved. Generated on Thu Mar 31 2022 01:12:29 |