Is there a way to stop and start a map/feature service in ArcGIS Server via python?
Lots of useful info has already been posted, it can just be a chore to sort through it to get exactly what you need. This is compounded by the fact that there are always many different ways to do the same thing and makes it extra confusing. Here is the Python code I use to start/stop a single service. It uses all built-in modules so there's no need to install anything extra.
<SPAN class="string token">"""Based on these 2014 resources by Kevin Hibma (Product Engineer at Esri): ArcGIS Server Administration Toolkit - 10.1+ <SPAN> </SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fwww.arcgis.com%2Fhome%2Fitem.html%3Fid%3D12dde73e0e784e47818162b4d41ee340" target="_blank">http://www.arcgis.com/home/item.html?id=12dde73e0e784e47818162b4d41ee340</A> AdministeringArcGISServerwithPython_DS2014 <SPAN> </SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fgithub.com%2Farcpy%2FAdministeringArcGISServerwithPython_DS2014" target="_blank">https://github.com/arcpy/AdministeringArcGISServerwithPython_DS2014</A> Other related information can be found in the ArcGIS Help Resources Scripting with the ArcGIS REST API <SPAN> </SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fresources.arcgis.com%2Fen%2Fhelp%2Fmain%2F10.2%2F0154%2F0154000005r1000000.htm" target="_blank">http://resources.arcgis.com/en/help/main/10.2/0154/0154000005r1000000.htm</A> """</SPAN> <SPAN class="comment token"># Required imports</SPAN> <SPAN class="keyword token">import</SPAN> urllib <SPAN class="keyword token">import</SPAN> urllib2 <SPAN class="keyword token">import</SPAN> json <SPAN class="keyword token">import</SPAN> contextlib <SPAN class="comment token">## context manager to clean up resources when exiting a 'with' block</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">## Entry point into the script</SPAN> <SPAN class="comment token"># Local variables</SPAN> <SPAN class="comment token">## Authentication</SPAN> adminUser <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"someuser"</SPAN> adminPass <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"passw0rd"</SPAN> <SPAN class="comment token">## ArcGIS Server Machine</SPAN> server <SPAN class="operator token">=</SPAN> <SPAN class="string token">"SERVERNAME"</SPAN> port <SPAN class="operator token">=</SPAN> <SPAN class="string token">"6080"</SPAN> <SPAN class="comment token">## Services ("FolderName/ServiceName.ServiceType")</SPAN> svc <SPAN class="operator token">=</SPAN> <SPAN class="string token">"SampleWorldCities.MapServer"</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Get ArcGIS Server token</SPAN> expiration <SPAN class="operator token">=</SPAN> <SPAN class="number token">60</SPAN> <SPAN class="comment token">## Token timeout in minutes; default is 60 minutes.</SPAN> token <SPAN class="operator token">=</SPAN> getToken<SPAN class="punctuation token">(</SPAN>adminUser<SPAN class="punctuation token">,</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> expiration<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Perform action on service</SPAN> action <SPAN class="operator token">=</SPAN> <SPAN class="string token">"start"</SPAN> <SPAN class="comment token">## "start" or "stop"</SPAN> jsonOuput <SPAN class="operator token">=</SPAN> serviceStartStop<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> svc<SPAN class="punctuation token">,</SPAN> action<SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">## Validate JSON object result</SPAN> <SPAN class="keyword token">if</SPAN> jsonOuput<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">"success"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"{} {} successful"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>action<SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> str<SPAN class="punctuation token">(</SPAN>svc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Failed to {} {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>action<SPAN class="punctuation token">,</SPAN> str<SPAN class="punctuation token">(</SPAN>svc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">raise</SPAN> Exception<SPAN class="punctuation token">(</SPAN>jsonOuput<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN> Exception<SPAN class="punctuation token">,</SPAN> err<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> err <SPAN class="comment token"># Function to generate a token from ArcGIS Server; returns token.</SPAN> <SPAN class="comment token"><SPAN>## </SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fresources.arcgis.com%2Fen%2Fhelp%2Farcgis-rest-api%2F02r3%2F02r3000000m5000000.htm" target="_blank">http://resources.arcgis.com/en/help/arcgis-rest-api/02r3/02r3000000m5000000.htm</A></SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">getToken</SPAN><SPAN class="punctuation token">(</SPAN>adminUser<SPAN class="punctuation token">,</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> expiration<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Build URL</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>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Encode the query string</SPAN> query_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="string token">'username'</SPAN><SPAN class="punctuation token">:</SPAN> adminUser<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'password'</SPAN><SPAN class="punctuation token">:</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'expiration'</SPAN><SPAN class="punctuation token">:</SPAN> str<SPAN class="punctuation token">(</SPAN>expiration<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="comment token">## Token timeout in minutes; default is 60 minutes.</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> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Request the token</SPAN> <SPAN class="keyword token">with</SPAN> contextlib<SPAN class="punctuation token">.</SPAN>closing<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> <SPAN class="keyword token">as</SPAN> jsonResponse<SPAN class="punctuation token">:</SPAN> getTokenResult <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>jsonResponse<SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">## Validate result</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> getTokenResult <SPAN class="operator token">or</SPAN> getTokenResult <SPAN class="operator token">==</SPAN> None<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">raise</SPAN> Exception<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Failed to get token: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>getTokenResult<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'messages'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> getTokenResult<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> e<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">raise</SPAN> Exception<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Could not connect to machine {} on port {}\n{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> e<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Function to start or stop a service on ArcGIS Server; returns JSON response.</SPAN> <SPAN class="comment token"><SPAN>## </SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fresources.arcgis.com%2Fen%2Fhelp%2Farcgis-rest-api%2F02r3%2F02r3000001s6000000.htm" target="_blank">http://resources.arcgis.com/en/help/arcgis-rest-api/02r3/02r3000001s6000000.htm</A></SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">serviceStartStop</SPAN><SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> svc<SPAN class="punctuation token">,</SPAN> action<SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Build URL</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"</SPAN></SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">)</SPAN> requestURL <SPAN class="operator token">=</SPAN> url <SPAN class="operator token">+</SPAN> <SPAN class="string token">"/services/{}/{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>svc<SPAN class="punctuation token">,</SPAN> action<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Encode the query string</SPAN> query_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="string token">"token"</SPAN><SPAN class="punctuation token">:</SPAN> token<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"f"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"json"</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> <SPAN class="comment token"># Send the server request and return the JSON response</SPAN> <SPAN class="keyword token">with</SPAN> contextlib<SPAN class="punctuation token">.</SPAN>closing<SPAN class="punctuation token">(</SPAN>urllib<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>requestURL<SPAN class="punctuation token">,</SPAN> query_string<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> jsonResponse<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>jsonResponse<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> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN> main<SPAN class="punctuation token">(</SPAN><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>
Yes, valid certificates are on the server and the user specified in the code does have full permissions.
I don't know much about SSL and certificates, but maybe you can find an answer here.
I am not too familiar with this code snip as I received it through this post....but seems that its trying to authenticate a certificate on the server....
do you have valid certificates on the server?
Does the user specified have the appropriate permissions to do this task?
Callum Smith any thoughts?
Thank you all for posting to this thread. It has been extremely useful for me. I did have one question regarding an error that I am receiving. When I run my script, it appears to run successfully until I hit this part of the code:
try: # Request the token with contextlib.closing(urllib2.urlopen(url, query_string)) as jsonResponse: getTokenResult = json.loads(jsonResponse.read()) ## Validate result if "token" not in getTokenResult or getTokenResult == None: raise Exception("Failed to get token: {}".format(getTokenResult['messages'])) else: return getTokenResult['token'] except urllib2.URLError, e: raise Exception("Could not connect to machine {} on port {}\n{}".format(server, port, e))
Once it hits this section, I am getting the following error. Any assistance as to why I am getting this error would be greatly appreciate.
Could not connect to machine <myserver> on port <myport><urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>
Thanks in advance,
Craig
THANK YOU ALL SO VERY MUCH....SO VERY APPRECIATED.....Have a great thanksgiving...cheers
Golden.....got it to work....THANK YOU ALL...went with Blakes solution...
<SPAN class="string token">"""Based on these 2014 resources by Kevin Hibma (Product Engineer at Esri): ArcGIS Server Administration Toolkit - 10.1+ <A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fwww.arcgis.com%2Fhome%2Fitem.html%3Fid%3D12dde73e0e784e47818162b4d41ee340" target="_blank">http://www.arcgis.com/home/item.html?id=12dde73e0e784e47818162b4d41ee340</A> AdministeringArcGISServerwithPython_DS2014 <A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fgithub.com%2Farcpy%2FAdministeringArcGISServerwithPython_DS2014" target="_blank">https://github.com/arcpy/AdministeringArcGISServerwithPython_DS2014</A> Other related information can be found in the ArcGIS Help Resources Scripting with the ArcGIS REST API <A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fresources.arcgis.com%2Fen%2Fhelp%2Fmain%2F10.2%2F0154%2F0154000005r1000000.htm" target="_blank">http://resources.arcgis.com/en/help/main/10.2/0154/0154000005r1000000.htm</A> """</SPAN> <SPAN class="comment token"># Required imports</SPAN> <SPAN class="keyword token">import</SPAN> urllib <SPAN class="keyword token">import</SPAN> urllib2 <SPAN class="keyword token">import</SPAN> json <SPAN class="keyword token">import</SPAN> contextlib <SPAN class="comment token">## context manager to clean up resources when exiting a 'with' block</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">## Entry point into the script</SPAN> <SPAN class="comment token"># Local variables</SPAN> <SPAN class="comment token">## Authentication</SPAN> adminUser <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"xyz"</SPAN> adminPass <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"xyz"</SPAN> <SPAN class="comment token">## ArcGIS Server Machine</SPAN> server <SPAN class="operator token">=</SPAN> <SPAN class="string token">"xxxxdev.xyz.xyz.gov"</SPAN> port <SPAN class="operator token">=</SPAN> <SPAN class="string token">"443"</SPAN> <SPAN class="comment token">## Services ("FolderName/ServiceName.ServiceType")</SPAN> svc <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Test/xInspection.MapServer"</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Get ArcGIS Server token</SPAN> expiration <SPAN class="operator token">=</SPAN> <SPAN class="number token">60</SPAN> <SPAN class="comment token">## Token timeout in minutes; default is 60 minutes.</SPAN> token <SPAN class="operator token">=</SPAN> getToken<SPAN class="punctuation token">(</SPAN>adminUser<SPAN class="punctuation token">,</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> expiration<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Perform action on service</SPAN> action <SPAN class="operator token">=</SPAN> <SPAN class="string token">"stop"</SPAN> <SPAN class="comment token">## "start" or "stop"</SPAN> jsonOuput <SPAN class="operator token">=</SPAN> serviceStartStop<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> svc<SPAN class="punctuation token">,</SPAN> action<SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">## Validate JSON object result</SPAN> <SPAN class="keyword token">if</SPAN> jsonOuput<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">"success"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"{} {} successful"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>action<SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> str<SPAN class="punctuation token">(</SPAN>svc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Failed to {} {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>action<SPAN class="punctuation token">,</SPAN> str<SPAN class="punctuation token">(</SPAN>svc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">raise</SPAN> Exception<SPAN class="punctuation token">(</SPAN>jsonOuput<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN> Exception<SPAN class="punctuation token">,</SPAN> err<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> err <SPAN class="comment token"># Function to generate a token from ArcGIS Server; returns token.</SPAN> <SPAN class="comment token"><SPAN>## </SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fresources.arcgis.com%2Fen%2Fhelp%2Farcgis-rest-api%2F02r3%2F02r3000000m5000000.htm" target="_blank">http://resources.arcgis.com/en/help/arcgis-rest-api/02r3/02r3000000m5000000.htm</A></SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">getToken</SPAN><SPAN class="punctuation token">(</SPAN>adminUser<SPAN class="punctuation token">,</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> expiration<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Build URL</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=https%3A%2F%2F" target="_blank">https://</A><SPAN>{}:{}/arcgis/admin/generateToken?f=json"</SPAN></SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Encode the query string</SPAN> query_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="string token">'username'</SPAN><SPAN class="punctuation token">:</SPAN> adminUser<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'password'</SPAN><SPAN class="punctuation token">:</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'expiration'</SPAN><SPAN class="punctuation token">:</SPAN> str<SPAN class="punctuation token">(</SPAN>expiration<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="comment token">## Token timeout in minutes; default is 60 minutes.</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> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Request the token</SPAN> <SPAN class="keyword token">with</SPAN> contextlib<SPAN class="punctuation token">.</SPAN>closing<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> <SPAN class="keyword token">as</SPAN> jsonResponse<SPAN class="punctuation token">:</SPAN> getTokenResult <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>jsonResponse<SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">## Validate result</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> getTokenResult <SPAN class="operator token">or</SPAN> getTokenResult <SPAN class="operator token">==</SPAN> None<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">raise</SPAN> Exception<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Failed to get token: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>getTokenResult<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'messages'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> getTokenResult<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> e<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">raise</SPAN> Exception<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Could not connect to machine {} on port {}\n{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> e<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Function to start or stop a service on ArcGIS Server; returns JSON response.</SPAN> <SPAN class="comment token"><SPAN>## </SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fresources.arcgis.com%2Fen%2Fhelp%2Farcgis-rest-api%2F02r3%2F02r3000001s6000000.htm" target="_blank">http://resources.arcgis.com/en/help/arcgis-rest-api/02r3/02r3000001s6000000.htm</A></SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">serviceStartStop</SPAN><SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> svc<SPAN class="punctuation token">,</SPAN> action<SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Build URL</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=https%3A%2F%2F" target="_blank">https://</A><SPAN>{}:{}/arcgis/admin"</SPAN></SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">)</SPAN> requestURL <SPAN class="operator token">=</SPAN> url <SPAN class="operator token">+</SPAN> <SPAN class="string token">"/services/{}/{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>svc<SPAN class="punctuation token">,</SPAN> action<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Encode the query string</SPAN> query_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="string token">"token"</SPAN><SPAN class="punctuation token">:</SPAN> token<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"f"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"json"</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> <SPAN class="comment token"># Send the server request and return the JSON response</SPAN> <SPAN class="keyword token">with</SPAN> contextlib<SPAN class="punctuation token">.</SPAN>closing<SPAN class="punctuation token">(</SPAN>urllib<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>requestURL<SPAN class="punctuation token">,</SPAN> query_string<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> jsonResponse<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>jsonResponse<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> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN> main<SPAN class="punctuation token">(</SPAN><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>
Blake, if you get anxious and want to take a look at my scripts, DM me. The issue is I'm writeing/testing for an internal addin and I have too much hardcoded (i.e. my .fgdb for example) for use in this addin. But the functionality is there and working. For GP services it actually has to read from the manifest.json for the service on the c: drive to see if it is being used. Some of these things I still need to pull out to be separate functions....but off that project for a couple weeks right now.
my modified toolset allows me to search the running services that are using a datasource so I shut down all those. Then I totally replace the fgdb before starting those services back up. I will eventually post it, but not ready yet.
Looking forward to that!
Agreed. Everything you need should be on this posting. Basically as Blake has eluded to there are many different ways to achieve what you want. Its just a matter of working through the options and using the one you are most comfortable with.
cheers
Callum
Error:Traceback (most recent call last): File "C:\Users\adminjk\Desktop\PythonSync\ServerAdminToolkit\StopService.py", line 9, in <module> import requestsImportError: No module named requests>>>
sys.path.append(r'\\works.co.nz\app\GISProjects\scripts\Dev\Framework\Modules')import requestsrequests.packages.urllib3.disable_warnings()logging.getLogger('requests.packages.urllib3').setLevel(logging.CRITICAL)
Jay have a look at the following code. This is something I have just cobbled together quickly. There are obviously "more that one way to skin a cat" so please dont take this as gospel.
All you should have to do is:
1. install the request module.
2. Enter a username and password for you server environment. i.e. what you use to log into arcgis server manager.
3. Enter a token url. This will be either a server a portal token depending if your environment is federated or not.
4. A admin url for your service you want to stop.
<SPAN class="comment token"># Stop Service</SPAN> <SPAN class="keyword token">import</SPAN> sys <SPAN class="keyword token">import</SPAN> os <SPAN class="keyword token">import</SPAN> logging <SPAN class="keyword token">import</SPAN> json sys<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">'\\works.co.nz\app\GISProjects\scripts\Dev\Framework\Modules'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">import</SPAN> requests requests<SPAN class="punctuation token">.</SPAN>packages<SPAN class="punctuation token">.</SPAN>urllib3<SPAN class="punctuation token">.</SPAN>disable_warnings<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> logging<SPAN class="punctuation token">.</SPAN>getLogger<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'requests.packages.urllib3'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>setLevel<SPAN class="punctuation token">(</SPAN>logging<SPAN class="punctuation token">.</SPAN>CRITICAL<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Parameters</SPAN> username <SPAN class="operator token">=</SPAN> <SPAN class="string token">''</SPAN> <SPAN class="comment token"># Username for your Server</SPAN> password <SPAN class="operator token">=</SPAN> <SPAN class="string token">''</SPAN> <SPAN class="comment token"># PAssword for your Server</SPAN> tokenURL <SPAN class="operator token">=</SPAN> <SPAN class="string token">''</SPAN> <SPAN class="comment token"># URL to generate token (Portal or server depending if federated or not)</SPAN> serviceURL <SPAN class="operator token">=</SPAN> <SPAN class="string token">''</SPAN> <SPAN class="comment token"># URL for service to stop</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Generate a Token</SPAN> payload <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">'referer'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token"><SPAN>'</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fwww.google.com" target="_blank">https://www.google.com</A><SPAN>'</SPAN></SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'request'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'gettoken'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'f'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'json'</SPAN> <SPAN class="punctuation token">}</SPAN> r <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>requests<SPAN class="punctuation token">.</SPAN>post<SPAN class="punctuation token">(</SPAN>tokenURL<SPAN class="punctuation token">,</SPAN> data<SPAN class="operator token">=</SPAN>payload<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># If valid token</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="string token">'token'</SPAN> <SPAN class="keyword token">in</SPAN> r<SPAN class="punctuation token">:</SPAN> token <SPAN class="operator token">=</SPAN> r<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'token'</SPAN><SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Now Stop the Service</SPAN> url <SPAN class="operator token">=</SPAN> serviceURL <SPAN class="operator token">+</SPAN> <SPAN class="string token">'/'</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'stop'</SPAN> payload <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">'f'</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">'json'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'token'</SPAN><SPAN class="punctuation token">:</SPAN>token<SPAN class="punctuation token">}</SPAN> r <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>requests<SPAN class="punctuation token">.</SPAN>post<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">,</SPAN> data<SPAN class="operator token">=</SPAN>payload<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> r<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'status'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'Service Stopped Sucessfully.'</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'Error encountered Stopping Service'</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'Unable to generate Token'</SPAN> <SPAN class="keyword token">except</SPAN> Exception<SPAN class="punctuation token">,</SPAN> e<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> e<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>
Im trying to below....BUT I only want to hit a single service....
In ArcGIS Server Manager Site:Folder X Individual Service: D
Not sure if I can configure that in the ServiceList parameter which is supported to follow format:
serviceList = List of services. A service must be in the <name>.<type> notation
Dont know my port number or where to find it
<SPAN class="string token">''' This script will stop or start all selected services ==Inputs== ServerName Port AdminUser AdminPassword (sent in clear text) Stop or Start Service(s) (multivalue list) '''</SPAN> <SPAN class="keyword token">import</SPAN> urllib<SPAN class="punctuation token">,</SPAN> urllib2<SPAN class="punctuation token">,</SPAN> json <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">def</SPAN> <SPAN class="token function">gentoken</SPAN><SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> adminUser<SPAN class="punctuation token">,</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> expiration<SPAN class="operator token">=</SPAN><SPAN class="number token">60</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#Re-usable function to get a token required for Admin changes</SPAN> query_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">'username'</SPAN><SPAN class="punctuation token">:</SPAN> adminUser<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'password'</SPAN><SPAN class="punctuation token">:</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'expiration'</SPAN><SPAN class="punctuation token">:</SPAN> str<SPAN class="punctuation token">(</SPAN>expiration<SPAN class="punctuation token">)</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"</SPAN></SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"><SPAN>#</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fvafwisdev.dgif.virginia.gov%2Farcgis%2Fadmin" target="_blank">https://vafwisdev.dgif.virginia.gov/arcgis/admin</A></SPAN> token <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>urllib<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>url <SPAN class="operator token">+</SPAN> <SPAN class="string token">"?f=json"</SPAN><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="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> quit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</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">def</SPAN> <SPAN class="token function">stopStartServices</SPAN><SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> adminUser<SPAN class="punctuation token">,</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> stopStart<SPAN class="punctuation token">,</SPAN> serviceList<SPAN class="punctuation token">,</SPAN> token<SPAN class="operator token">=</SPAN>None<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">''' Function to stop, start or delete a service. Requires Admin user/password, as well as server and port (necessary to construct token if one does not exist). stopStart = Stop|Start|Delete serviceList = List of services. A service must be in the <name>.<type> notation If a token exists, you can pass one in for use. '''</SPAN> <SPAN class="comment token"># Get and set the token</SPAN> <SPAN class="keyword token">if</SPAN> token <SPAN class="keyword token">is</SPAN> None<SPAN class="punctuation token">:</SPAN> token <SPAN class="operator token">=</SPAN> gentoken<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> adminUser<SPAN class="punctuation token">,</SPAN> adminPass<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Getting services from tool validation creates a semicolon delimited list that needs to be broken up</SPAN> services <SPAN class="operator token">=</SPAN> serviceList<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">#modify the services(s) </SPAN> <SPAN class="keyword token">for</SPAN> service <SPAN class="keyword token">in</SPAN> services<SPAN class="punctuation token">:</SPAN> service <SPAN class="operator token">=</SPAN> urllib<SPAN class="punctuation token">.</SPAN>quote<SPAN class="punctuation token">(</SPAN>service<SPAN class="punctuation token">.</SPAN>encode<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'utf8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</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>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> service<SPAN class="punctuation token">,</SPAN> stopStart<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="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>str<SPAN class="punctuation token">(</SPAN>service<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">" === "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>stopStart<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddWarning<SPAN class="punctuation token">(</SPAN>status<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</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"># Gather inputs </SPAN> server <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> port <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> adminUser <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN> adminPass <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN> stopStart <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameter<SPAN class="punctuation token">(</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">)</SPAN> serviceList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN> stopStartServices<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> adminUser<SPAN class="punctuation token">,</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> stopStart<SPAN class="punctuation token">,</SPAN> serviceList<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>
Hi Jay
Not quite that simple. You will have to generate a token and pass that as part of the request. Are you familiar with making http/https requests with python? Have a look at the request module. If I have time today I will put together an example script.
If you download that admin toolbox, use the tool. One you put in your server name port (usually 6080) and admin credentials, it should list ALL the services, where running or not. That is why I'm modifying it. Cause otherwise when you rerun it to start the services back up, it will include anything your checked to "stop" whether or not is was running before.
That might not make sense, but just run it so you get the GUI, and it should make more sense.
Was looking into the AGS Admin link...saw the below.
Not sure where to download all the scripts and stuff from the Zip file too?
Can I specify a single Service?
Pretty confused right now.....
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">stopStartServices</SPAN><SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> adminUser<SPAN class="punctuation token">,</SPAN> adminPass<SPAN class="punctuation token">,</SPAN> stopStart<SPAN class="punctuation token">,</SPAN> serviceList<SPAN class="punctuation token">,</SPAN> token<SPAN class="operator token">=</SPAN>None<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">''' Function to stop, start or delete a service. Requires Admin user/password, as well as server and port (necessary to construct token if one does not exist). stopStart = Stop|Start|Delete serviceList = List of services. A service must be in the <name>.<type> notation If a token exists, you can pass one in for use. '''</SPAN> <SPAN class="comment token"># Get and set the token</SPAN> <SPAN class="keyword token">if</SPAN> token <SPAN class="keyword token">is</SPAN> None<SPAN class="punctuation token">:</SPAN> token <SPAN class="operator token">=</SPAN> gentoken<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> adminUser<SPAN class="punctuation token">,</SPAN> adminPass<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># modify the services(s) </SPAN> <SPAN class="keyword token">for</SPAN> service <SPAN class="keyword token">in</SPAN> serviceList<SPAN class="punctuation token">:</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>server<SPAN class="punctuation token">,</SPAN> port<SPAN class="punctuation token">,</SPAN> service<SPAN class="punctuation token">,</SPAN> stopStart<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="keyword token">if</SPAN> <SPAN class="string token">'success'</SPAN> <SPAN class="keyword token">in</SPAN> status<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>service<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">" === "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>stopStart<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> status <SPAN class="keyword token">return</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>
So I only need one line of code?
<SPAN class="" style="border: 0px; font-size: 11.44px;">http</SPAN><SPAN class="" style="border: 0px; font-size: 11.44px;">:</SPAN><SPAN class="" style="color: #008000; border: 0px; font-size: 11.44px;">//server:port/arcgis/admin/services/Maps/SeattleMap.MapServer/start</SPAN>
What about credentials etc. and the 100 lines of code in the previous posts.
Yes you can using the REST api.
See ArcGIS REST API
yes.
The "released" version of the toolbox is
Downloadable tools for ArcGIS Server administration | ArcGIS Blog
direct link to download page... http://www.arcgis.com/home/item.html?id=12dde73e0e784e47818162b4d41ee340
Might want to look at this thread too
AGS admin scripting help
I have done some additional work on a copy locally that reads the credentials from a .ini file so it doesn't echo the admin credentials in the results tab. Also, I know Kevin Hibma is working on a new version, but haven't heard any release date (I think it's a, "as I have time" project)
edit: btw, my modified toolset allows me to search the running services that are using a datasource so I shut down all those. Then I totally replace the fgdb before starting those services back up. I will eventually post it, but not ready yet.
NOTE: I am trying to do this from a different server from where AGS is installed.
I have to stop a service to run some code on the FC in SDe. Then restart the Service.
Can I call a python script on another server?
Can I do this via arcpy?
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.