Programmatically interacting with any administrative API is and will continue to be an integral part of the software. HTTPS doesn't necessarily mean they're secured, at least in the same sense that you'd say you need a token to reach them. It simply means that the traffic to reach them is encrypted.
In order to reach an HTTPS endpoint via Python, the certificate used for the endpoint needs to be trusted by Python or an untrusted certificate needs to be ignored. This isn't specific to Esri, ArcGIS Server, arcpy, etc. Server, even at 10.7, can be HTTPS and HTTP, but by default on a new site, it is HTTPS. Ideally, the certificate is already trusted by Python, or with a bit of work, you can configure Python to trust your own certificates:
https://stackoverflow.com/questions/39356413/how-to-add-a-custom-ca-root-certificate-to-the-ca-store...
The following script uses urllib and urllib2 instead of the httplib library to make web requests and disables certificate validation:
import urllib, urllib2, json,ssl
baseURL = 'https://server.domain.com:6443/arcgis'
username = 'admin'
password = 'admin'
whatToDo = "start"
def openURL(url,params=None, protocol=None):
try:
if params:
params.update(dict(f="json"))
else:
params = dict(f="json")
if protocol:
encoded_params = str.encode(urllib.urlencode(params))
encoded_params = encoded_params.decode("utf-8")
url = "{0}?{1}".format(url, encoded_params)
request = urllib2.Request(url)
sslContext = ssl._create_unverified_context()
request.add_header('referer',baseURL)
response = urllib2.urlopen(request, context=sslContext)
else:
encoded_params = str.encode(urllib.urlencode(params))
request = urllib2.Request(url)
sslContext = ssl._create_unverified_context()
request.add_header('referer',baseURL)
response = urllib2.urlopen(request,encoded_params,context=sslContext)
decodedResponse = response.read().decode('utf-8')
jsonResponse = json.loads(decodedResponse)
return jsonResponse
except urllib2.HTTPError as e:
return e
except urllib2.URLError as e:
return e
except Exception as e:
print(e)
def createToken(baseURL,username,password):
tokenURL = "{}/tokens/generateToken".format(baseURL)
params = {"username":username,
"password":password,
"client":'referer',
"referer":baseURL}
token = openURL(tokenURL,params)['token']
return token
'''Function to stop or start services'''
def startStopService(serviceURL):
serviceName = urllib2.urlparse.urlparse(serviceURL).path.split("/")[-1]
resp = openURL("{}/{}".format(serviceURL,whatToDo),params)
if "status" in resp and resp['status'] == 'success':
print("Successfully {} {}".format(msg,serviceName))
else:
print("Unable to {} {}.\n {}".format(whatToDo,serviceName,resp))
token = createToken(baseURL,username,password)
params = {"token":token}
servicesURL = '{}/admin/services'.format(baseURL)
root = openURL(servicesURL,params)
if whatToDo == "stop":
msg = "stopped"
else:
msg = "started"
for service in root['services']:
serviceURL = '{}/{}.{}'.format(servicesURL,service['serviceName'],service['type'])
startStopService(serviceURL)
for folder in root['folders']:
if not folder in ['System','Utilities']:
folderURL = "{}/{}".format(servicesURL,folder)
folderServices = openURL(folderURL,params)
for service in folderServices['services']:
serviceURL = '{}/{}/{}.{}'.format(servicesURL,folder,service['serviceName'],service['type'])
startStopService(serviceURL)
https://community.netapp.com/t5/Software-Development-Kit-SDK-and-API-Discussions/Python-How-to-disab...
You can do this with the requests module as well:
https://stackoverflow.com/questions/15445981/how-do-i-disable-the-security-certificate-check-in-pyth...
I'll reiterate that the preferred solution isn't disabling certificate validation, though.