I'm trying to run a python scrtipt to stop services, but I'm getting the following error message : "No JSON object could be decoded" . My server uses https protocol thru port 6443. I'm using the file "StartStopServices.py" available at following path: https://github.com/WestonSF/ArcGISAdminToolkit/commit/a183df77441956e515105b2b166b11eb4e525873
<SPAN class="comment token">#-------------------------------------------------------------</SPAN>
<SPAN class="comment token"># Name: Start or Stop Services</SPAN>
<SPAN class="comment token"># Purpose: Starts or stops GIS services. Can start/stop all GIS services for an ArcGIS server site or just the ones</SPAN>
<SPAN class="comment token"># that are specified.</SPAN>
<SPAN class="comment token"><SPAN># Author: Shaun Weston (</SPAN><A class="jive-link-email-small" href="mailto:shaun_weston@eagle.co.nz" rel="nofollow noopener noreferrer" target="_blank">shaun_weston@eagle.co.nz</A><SPAN>)</SPAN></SPAN>
<SPAN class="comment token"># Date Created: 07/10/2014</SPAN>
<SPAN class="comment token"># Last Updated: 06/11/2014</SPAN>
<SPAN class="comment token"># Copyright: (c) Eagle Technology</SPAN>
<SPAN class="comment token"># ArcGIS Version: 10.1+</SPAN>
<SPAN class="comment token"># Python Version: 2.7</SPAN>
<SPAN class="comment token">#--------------------------------</SPAN>
<SPAN class="comment token"># Import modules</SPAN>
<SPAN class="keyword token">import</SPAN> os
<SPAN class="keyword token">import</SPAN> sys
<SPAN class="keyword token">import</SPAN> logging
<SPAN class="keyword token">import</SPAN> smtplib
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> httplib
<SPAN class="keyword token">import</SPAN> json
<SPAN class="keyword token">import</SPAN> urllib
<SPAN class="keyword token">import</SPAN> urllib2
<SPAN class="keyword token">import</SPAN> urlparse
<SPAN class="comment token"># Enable data to be overwritten</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token"># Set global variables</SPAN>
enableLogging <SPAN class="operator token">=</SPAN> <SPAN class="string token">"false"</SPAN> <SPAN class="comment token"># Use logger.info("Example..."), logger.warning("Example..."), logger.error("Example...")</SPAN>
logFile <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN> <SPAN class="comment token"># os.path.join(os.path.dirname(__file__), "Example.log")</SPAN>
sendErrorEmail <SPAN class="operator token">=</SPAN> <SPAN class="string token">"false"</SPAN>
emailTo <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN>
emailUser <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN>
emailPassword <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN>
emailSubject <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN>
emailMessage <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN>
output <SPAN class="operator token">=</SPAN> None
<SPAN class="comment token"># Start of main function</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">mainFunction</SPAN><SPAN class="punctuation token">(</SPAN>agsServerSite<SPAN class="punctuation token">,</SPAN>username<SPAN class="punctuation token">,</SPAN>password<SPAN class="punctuation token">,</SPAN>startStop<SPAN class="punctuation token">,</SPAN>allServices<SPAN class="punctuation token">,</SPAN>userServices<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Get parameters from ArcGIS Desktop tool by seperating by comma e.g. (var1 is 1st parameter,var2 is 2nd parameter,var3 is 3rd parameter) </SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Setup logging</SPAN>
logger<SPAN class="punctuation token">,</SPAN> logMessage <SPAN class="operator token">=</SPAN> setLogging<SPAN class="punctuation token">(</SPAN>logFile<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Log start of process</SPAN>
logger<SPAN class="punctuation token">.</SPAN>info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Process started."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># --------------------------------------- Start of code --------------------------------------- #</SPAN>
<SPAN class="comment token"># Get the server site details</SPAN>
protocol<SPAN class="punctuation token">,</SPAN> serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">,</SPAN> context <SPAN class="operator token">=</SPAN> splitSiteURL<SPAN class="punctuation token">(</SPAN>agsServerSite<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># If any of the variables are blank</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>serverName <SPAN class="operator token">==</SPAN> None <SPAN class="operator token">or</SPAN> serverPort <SPAN class="operator token">==</SPAN> None <SPAN class="operator token">or</SPAN> protocol <SPAN class="operator token">==</SPAN> None <SPAN class="operator token">or</SPAN> context <SPAN class="operator token">==</SPAN> None<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN>
<SPAN class="comment token"># Add on slash to context if necessary</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> context<SPAN class="punctuation token">.</SPAN>endswith<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'/'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
context <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'/'</SPAN>
<SPAN class="comment token"># Add on admin to context if necessary </SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> context<SPAN class="punctuation token">.</SPAN>endswith<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'admin/'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
context <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'admin/'</SPAN>
<SPAN class="comment token"># Get token</SPAN>
token <SPAN class="operator token">=</SPAN> getToken<SPAN class="punctuation token">(</SPAN>username<SPAN class="punctuation token">,</SPAN> password<SPAN class="punctuation token">,</SPAN> serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># If token received</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>token <SPAN class="operator token">!=</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
services <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
folder <SPAN class="operator token">=</SPAN> <SPAN class="string token">''</SPAN>
<SPAN class="comment token"># If task is for all services</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>allServices <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Query the root level for services </SPAN>
url <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2F" target="_blank">http://</A><SPAN>{}:{}/arcgis/admin/services{}?f=pjson&token={}"</SPAN></SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">,</SPAN> folder<SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Make the request </SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
serviceList <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>urllib2<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> urllib2<SPAN class="punctuation token">.</SPAN>URLError<SPAN class="punctuation token">,</SPAN> error<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>error<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN>error<SPAN class="punctuation token">)</SPAN>
sys<SPAN class="punctuation token">.</SPAN>exit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Add the services at the root level</SPAN>
<SPAN class="keyword token">for</SPAN> single <SPAN class="keyword token">in</SPAN> serviceList<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"services"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN>
services<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>single<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'serviceName'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.'</SPAN> <SPAN class="operator token">+</SPAN> single<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'type'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Build up list of folders and remove the System and Utilities folder</SPAN>
folderList <SPAN class="operator token">=</SPAN> serviceList<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"folders"</SPAN><SPAN class="punctuation token">]</SPAN>
folderList<SPAN class="punctuation token">.</SPAN>remove<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Utilities"</SPAN><SPAN class="punctuation token">)</SPAN>
folderList<SPAN class="punctuation token">.</SPAN>remove<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"System"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># If there is any folders</SPAN>
<SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>folderList<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># For each folder in the list</SPAN>
<SPAN class="keyword token">for</SPAN> folder <SPAN class="keyword token">in</SPAN> folderList<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Query the folder for map services</SPAN>
URL <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2F" target="_blank">http://</A><SPAN>{}:{}/arcgis/admin/services/{}?f=pjson&token={}"</SPAN></SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">,</SPAN> folder<SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">)</SPAN>
fList <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>urllib2<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>URL<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> single <SPAN class="keyword token">in</SPAN> fList<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"services"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Add the services found to the list</SPAN>
services<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>folder <SPAN class="operator token">+</SPAN> <SPAN class="string token">"/"</SPAN> <SPAN class="operator token">+</SPAN> single<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'serviceName'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.'</SPAN> <SPAN class="operator token">+</SPAN> single<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'type'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Just for the user defined list of services</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
services <SPAN class="operator token">=</SPAN> userServices<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">","</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># For each of the services</SPAN>
<SPAN class="keyword token">for</SPAN> service <SPAN class="keyword token">in</SPAN> services<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Start or stop the map service</SPAN>
op_service_url <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2F" target="_blank">http://</A><SPAN>{}:{}/arcgis/admin/services/{}/{}?token={}&f=json"</SPAN></SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">,</SPAN> service<SPAN class="punctuation token">,</SPAN> startStop<SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">)</SPAN>
status <SPAN class="operator token">=</SPAN> urllib2<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>op_service_url<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">' '</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># If successfully started/stopped </SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="string token">'success'</SPAN> <SPAN class="keyword token">in</SPAN> status<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN>startStop <SPAN class="operator token">+</SPAN> <SPAN class="string token">" successfully performed on "</SPAN> <SPAN class="operator token">+</SPAN> service<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN>startStop <SPAN class="operator token">+</SPAN> <SPAN class="string token">" successfully performed on "</SPAN> <SPAN class="operator token">+</SPAN> service<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># If there is an error</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Failed to perform operation. Returned message from the server:"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>status<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Failed to perform operation. Returned message from the server:"</SPAN><SPAN class="punctuation token">)</SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN>status<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># --------------------------------------- End of code --------------------------------------- # </SPAN>
<SPAN class="comment token"># If called from gp tool return the arcpy parameter </SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Return the output if there is any</SPAN>
<SPAN class="keyword token">if</SPAN> output<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> output<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Otherwise return the result </SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Return the output if there is any</SPAN>
<SPAN class="keyword token">if</SPAN> output<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">return</SPAN> output
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Log end of process</SPAN>
logger<SPAN class="punctuation token">.</SPAN>info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Process ended."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Remove file handler and close log file </SPAN>
logging<SPAN class="punctuation token">.</SPAN>FileHandler<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN>logMessage<SPAN class="punctuation token">)</SPAN>
logger<SPAN class="punctuation token">.</SPAN>removeHandler<SPAN class="punctuation token">(</SPAN>logMessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">pass</SPAN>
<SPAN class="comment token"># If arcpy error</SPAN>
<SPAN class="keyword token">except</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ExecuteError<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Build and show the error message</SPAN>
errorMessage <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>errorMessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Log error </SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN>errorMessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Remove file handler and close log file</SPAN>
logging<SPAN class="punctuation token">.</SPAN>FileHandler<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN>logMessage<SPAN class="punctuation token">)</SPAN>
logger<SPAN class="punctuation token">.</SPAN>removeHandler<SPAN class="punctuation token">(</SPAN>logMessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>sendErrorEmail <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Send email</SPAN>
sendEmail<SPAN class="punctuation token">(</SPAN>errorMessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># If python error</SPAN>
<SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> e<SPAN class="punctuation token">:</SPAN>
errorMessage <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN>
<SPAN class="comment token"># Build and show the error message</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">.</SPAN>args<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>i <SPAN class="operator token">==</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
errorMessage <SPAN class="operator token">=</SPAN> unicode<SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">.</SPAN>args<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>encode<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'utf-8'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
errorMessage <SPAN class="operator token">=</SPAN> errorMessage <SPAN class="operator token">+</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="operator token">+</SPAN> unicode<SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">.</SPAN>args<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>encode<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'utf-8'</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>errorMessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Log error </SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN>errorMessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Remove file handler and close log file</SPAN>
logging<SPAN class="punctuation token">.</SPAN>FileHandler<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN>logMessage<SPAN class="punctuation token">)</SPAN>
logger<SPAN class="punctuation token">.</SPAN>removeHandler<SPAN class="punctuation token">(</SPAN>logMessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>sendErrorEmail <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Send email</SPAN>
sendEmail<SPAN class="punctuation token">(</SPAN>errorMessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># End of main function</SPAN>
<SPAN class="comment token"># Start of get token function</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">getToken</SPAN><SPAN class="punctuation token">(</SPAN>username<SPAN class="punctuation token">,</SPAN> password<SPAN class="punctuation token">,</SPAN> serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
query_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">'username'</SPAN><SPAN class="punctuation token">:</SPAN> username<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'password'</SPAN><SPAN class="punctuation token">:</SPAN> password<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'expiration'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"60"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'client'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'requestip'</SPAN><SPAN class="punctuation token">}</SPAN>
query_string <SPAN class="operator token">=</SPAN> urllib<SPAN class="punctuation token">.</SPAN>urlencode<SPAN class="punctuation token">(</SPAN>query_dict<SPAN class="punctuation token">)</SPAN>
url <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2F" target="_blank">http://</A><SPAN>{}:{}/arcgis/admin/generateToken?f=json"</SPAN></SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
token <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>urllib2<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">,</SPAN> query_string<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="string token">"token"</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> token <SPAN class="operator token">or</SPAN> token <SPAN class="operator token">==</SPAN> None<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Failed to get token, return message from server:"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>token<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'messages'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Failed to get token, return message from server:"</SPAN><SPAN class="punctuation token">)</SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN>token<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'messages'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
sys<SPAN class="punctuation token">.</SPAN>exit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Return the token to the function which called for it</SPAN>
<SPAN class="keyword token">return</SPAN> token<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'token'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">except</SPAN> urllib2<SPAN class="punctuation token">.</SPAN>URLError<SPAN class="punctuation token">,</SPAN> error<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Could not connect to machine {} on port {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>error<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Could not connect to machine {} on port {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN>error<SPAN class="punctuation token">)</SPAN>
sys<SPAN class="punctuation token">.</SPAN>exit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># End of get token function</SPAN>
<SPAN class="comment token"># Start of HTTP POST request to the server function</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">postToServer</SPAN><SPAN class="punctuation token">(</SPAN>serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">,</SPAN> protocol<SPAN class="punctuation token">,</SPAN> url<SPAN class="punctuation token">,</SPAN> params<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># If on standard port</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>serverPort <SPAN class="operator token">==</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN> <SPAN class="operator token">and</SPAN> protocol <SPAN class="operator token">==</SPAN> <SPAN class="string token">'http'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
serverPort <SPAN class="operator token">=</SPAN> <SPAN class="number token">80</SPAN>
<SPAN class="comment token"># If on secure port</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>serverPort <SPAN class="operator token">==</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN> <SPAN class="operator token">and</SPAN> protocol <SPAN class="operator token">==</SPAN> <SPAN class="string token">'https'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
serverPort <SPAN class="operator token">=</SPAN> <SPAN class="number token">6443</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>protocol <SPAN class="operator token">==</SPAN> <SPAN class="string token">'http'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
httpConn <SPAN class="operator token">=</SPAN> httplib<SPAN class="punctuation token">.</SPAN>HTTPConnection<SPAN class="punctuation token">(</SPAN>serverName<SPAN class="punctuation token">,</SPAN> int<SPAN class="punctuation token">(</SPAN>serverPort<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>protocol <SPAN class="operator token">==</SPAN> <SPAN class="string token">'https'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
httpConn <SPAN class="operator token">=</SPAN> httplib<SPAN class="punctuation token">.</SPAN>HTTPSConnection<SPAN class="punctuation token">(</SPAN>serverName<SPAN class="punctuation token">,</SPAN> int<SPAN class="punctuation token">(</SPAN>serverPort<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
headers <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"Content-type"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"application/x-www-form-urlencoded"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Accept"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"text/plain"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'referer'</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">'backuputility'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'referrer'</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">'backuputility'</SPAN><SPAN class="punctuation token">}</SPAN>
<SPAN class="comment token"># URL encode the resource URL</SPAN>
url <SPAN class="operator token">=</SPAN> urllib<SPAN class="punctuation token">.</SPAN>quote<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">.</SPAN>encode<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'utf-8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Build the connection to add the roles to the server</SPAN>
httpConn<SPAN class="punctuation token">.</SPAN>request<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"POST"</SPAN><SPAN class="punctuation token">,</SPAN> url<SPAN class="punctuation token">,</SPAN> params<SPAN class="punctuation token">,</SPAN> headers<SPAN class="punctuation token">)</SPAN>
response <SPAN class="operator token">=</SPAN> httpConn<SPAN class="punctuation token">.</SPAN>getresponse<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
data <SPAN class="operator token">=</SPAN> response<SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
httpConn<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Return response</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">(</SPAN>response<SPAN class="punctuation token">,</SPAN> data<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># End of HTTP POST request to the server function </SPAN>
<SPAN class="comment token"># Start of split URL function </SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">splitSiteURL</SPAN><SPAN class="punctuation token">(</SPAN>siteURL<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
serverName <SPAN class="operator token">=</SPAN> <SPAN class="string token">''</SPAN>
serverPort <SPAN class="operator token">=</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN>
protocol <SPAN class="operator token">=</SPAN> <SPAN class="string token">'http'</SPAN>
context <SPAN class="operator token">=</SPAN> <SPAN class="string token">'/arcgis'</SPAN>
<SPAN class="comment token"># Split up the URL provided</SPAN>
urllist <SPAN class="operator token">=</SPAN> urlparse<SPAN class="punctuation token">.</SPAN>urlsplit<SPAN class="punctuation token">(</SPAN>siteURL<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Put the URL list into a dictionary</SPAN>
d <SPAN class="operator token">=</SPAN> urllist<SPAN class="punctuation token">.</SPAN>_asdict<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Get the server name and port</SPAN>
serverNameAndPort <SPAN class="operator token">=</SPAN> d<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'netloc'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">":"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># User did not enter the port number, so we return -1</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>serverNameAndPort<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
serverName <SPAN class="operator token">=</SPAN> serverNameAndPort<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>serverNameAndPort<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
serverName <SPAN class="operator token">=</SPAN> serverNameAndPort<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
serverPort <SPAN class="operator token">=</SPAN> serverNameAndPort<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Get protocol</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>d<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'scheme'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">is</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="string token">''</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
protocol <SPAN class="operator token">=</SPAN> d<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'scheme'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Get path</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>d<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'path'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">is</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="string token">'/'</SPAN> <SPAN class="operator token">and</SPAN> d<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'path'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">is</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="string token">''</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
context <SPAN class="operator token">=</SPAN> d<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'path'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Return variables</SPAN>
<SPAN class="keyword token">return</SPAN> protocol<SPAN class="punctuation token">,</SPAN> serverName<SPAN class="punctuation token">,</SPAN> serverPort<SPAN class="punctuation token">,</SPAN> context
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"The ArcGIS Server site URL should be in the format http(s)://<host>:<port>/arcgis"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"The ArcGIS Server site URL should be in the format http(s)://<host>:<port>/arcgis"</SPAN><SPAN class="punctuation token">)</SPAN>
sys<SPAN class="punctuation token">.</SPAN>exit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> None<SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">,</SPAN> None
<SPAN class="comment token"># End of split URL function</SPAN>
<SPAN class="comment token"># Start of status check JSON object function</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">assertJsonSuccess</SPAN><SPAN class="punctuation token">(</SPAN>data<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
obj <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>data<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="string token">'status'</SPAN> <SPAN class="keyword token">in</SPAN> obj <SPAN class="operator token">and</SPAN> obj<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'status'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">"error"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'messages'</SPAN> <SPAN class="keyword token">in</SPAN> obj<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
errMsgs <SPAN class="operator token">=</SPAN> obj<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'messages'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> errMsg <SPAN class="keyword token">in</SPAN> errMsgs<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>errMsg<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Logging</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>enableLogging <SPAN class="operator token">==</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
logger<SPAN class="punctuation token">.</SPAN>error<SPAN class="punctuation token">(</SPAN>errMsg<SPAN class="punctuation token">)</SPAN>
sys<SPAN class="punctuation token">.</SPAN>exit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="token boolean">False</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token"># End of status check JSON object function</SPAN>
<SPAN class="comment token"># Start of set logging function</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">setLogging</SPAN><SPAN class="punctuation token">(</SPAN>logFile<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Create a logger</SPAN>
logger <SPAN class="operator token">=</SPAN> logging<SPAN class="punctuation token">.</SPAN>getLogger<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>basename<SPAN class="punctuation token">(</SPAN>__file__<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
logger<SPAN class="punctuation token">.</SPAN>setLevel<SPAN class="punctuation token">(</SPAN>logging<SPAN class="punctuation token">.</SPAN>DEBUG<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Setup log message handler</SPAN>
logMessage <SPAN class="operator token">=</SPAN> logging<SPAN class="punctuation token">.</SPAN>FileHandler<SPAN class="punctuation token">(</SPAN>logFile<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Setup the log formatting</SPAN>
logFormat <SPAN class="operator token">=</SPAN> logging<SPAN class="punctuation token">.</SPAN>Formatter<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%(asctime)s: %(levelname)s - %(message)s"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"%d/%m/%Y - %H:%M:%S"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Add formatter to log message handler</SPAN>
logMessage<SPAN class="punctuation token">.</SPAN>setFormatter<SPAN class="punctuation token">(</SPAN>logFormat<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Add log message handler to logger</SPAN>
logger<SPAN class="punctuation token">.</SPAN>addHandler<SPAN class="punctuation token">(</SPAN>logMessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> logger<SPAN class="punctuation token">,</SPAN> logMessage
<SPAN class="comment token"># End of set logging function</SPAN>
<SPAN class="comment token"># Start of send email function</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">sendEmail</SPAN><SPAN class="punctuation token">(</SPAN>message<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Send an email</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Sending email..."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Server and port information</SPAN>
smtpServer <SPAN class="operator token">=</SPAN> smtplib<SPAN class="punctuation token">.</SPAN>SMTP<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"smtp.gmail.com"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">587</SPAN><SPAN class="punctuation token">)</SPAN>
smtpServer<SPAN class="punctuation token">.</SPAN>ehlo<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
smtpServer<SPAN class="punctuation token">.</SPAN>starttls<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
smtpServer<SPAN class="punctuation token">.</SPAN>ehlo
<SPAN class="comment token"># Login with sender email address and password</SPAN>
smtpServer<SPAN class="punctuation token">.</SPAN>login<SPAN class="punctuation token">(</SPAN>emailUser<SPAN class="punctuation token">,</SPAN> emailPassword<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Email content</SPAN>
header <SPAN class="operator token">=</SPAN> <SPAN class="string token">'To:'</SPAN> <SPAN class="operator token">+</SPAN> emailTo <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\n'</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'From: '</SPAN> <SPAN class="operator token">+</SPAN> emailUser <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\n'</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'Subject:'</SPAN> <SPAN class="operator token">+</SPAN> emailSubject <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\n'</SPAN>
body <SPAN class="operator token">=</SPAN> header <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\n'</SPAN> <SPAN class="operator token">+</SPAN> emailMessage <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\n'</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\n'</SPAN> <SPAN class="operator token">+</SPAN> message
<SPAN class="comment token"># Send the email and close the connection</SPAN>
smtpServer<SPAN class="punctuation token">.</SPAN>sendmail<SPAN class="punctuation token">(</SPAN>emailUser<SPAN class="punctuation token">,</SPAN> emailTo<SPAN class="punctuation token">,</SPAN> body<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># End of send email function</SPAN>
<SPAN class="comment token"># This test allows the script to be used from the operating</SPAN>
<SPAN class="comment token"># system command prompt (stand-alone), in a Python IDE, </SPAN>
<SPAN class="comment token"># as a geoprocessing script tool, or as a module imported in</SPAN>
<SPAN class="comment token"># another script</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Arguments are optional - If running from ArcGIS Desktop tool, parameters will be loaded into *argv</SPAN>
argv <SPAN class="operator token">=</SPAN> tuple<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetArgumentCount<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
mainFunction<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>argv<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>