Hi,
I'm trying to list the properties of all the services from GIS Server using python.Is there any sample script with validating the user and password credentials?
ArcGIS API for Python | ArcGIS for Developers is expressly designed for web-GIS activities, have you looked at the Guide or any Sample Notebooks?
UPDATE: Sample code for listing all services and printing their JSON properties.
<SPAN class="keyword token">from</SPAN> getpass <SPAN class="keyword token">import</SPAN> getpass <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis<SPAN class="punctuation token">.</SPAN>server<SPAN class="punctuation token">.</SPAN>catalog <SPAN class="keyword token">import</SPAN> ServicesDirectory url <SPAN class="operator token">=</SPAN> <SPAN class="comment token"><SPAN># GIS server base URL, i.e., </SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2FFQDN%3A6443%2Farcgis" target="_blank">https://FQDN:6443/arcgis</A></SPAN> user <SPAN class="operator token">=</SPAN> <SPAN class="comment token"># admin username</SPAN> SD <SPAN class="operator token">=</SPAN> ServicesDirectory<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">,</SPAN> username<SPAN class="operator token">=</SPAN>user<SPAN class="punctuation token">,</SPAN> password<SPAN class="operator token">=</SPAN>getpass<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> verify_cert<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> dir <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">[</SPAN>None<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+</SPAN> SD<SPAN class="punctuation token">.</SPAN>folders<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> srv <SPAN class="keyword token">in</SPAN> SD<SPAN class="punctuation token">.</SPAN>list<SPAN class="punctuation token">(</SPAN>folder<SPAN class="operator token">=</SPAN>dir<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>srv<SPAN class="punctuation token">.</SPAN>properties<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>
Hi Manes,
You could try something like below. It will iterate through all the services and print the properties:
<SPAN class="keyword token">import</SPAN> urllib<SPAN class="punctuation token">,</SPAN> urllib2<SPAN class="punctuation token">,</SPAN> json <SPAN class="comment token"># Variables</SPAN> username <SPAN class="operator token">=</SPAN> <SPAN class="string token">'siteadmin'</SPAN> password <SPAN class="operator token">=</SPAN> <SPAN class="string token">'siteadmin'</SPAN> server <SPAN class="operator token">=</SPAN> <SPAN class="string token">"gis.esri.com"</SPAN> tokenURL <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>{}:6080/arcgis/tokens/'</SPAN></SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">)</SPAN> params <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">'pjson'</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">'client'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'requestip'</SPAN><SPAN class="punctuation token">}</SPAN> req <SPAN class="operator token">=</SPAN> urllib2<SPAN class="punctuation token">.</SPAN>Request<SPAN class="punctuation token">(</SPAN>tokenURL<SPAN class="punctuation token">,</SPAN> urllib<SPAN class="punctuation token">.</SPAN>urlencode<SPAN class="punctuation token">(</SPAN>params<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> response <SPAN class="operator token">=</SPAN> urllib2<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>req<SPAN class="punctuation token">)</SPAN> data <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>load<SPAN class="punctuation token">(</SPAN>response<SPAN class="punctuation token">)</SPAN> token <SPAN class="operator token">=</SPAN> data<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'token'</SPAN><SPAN class="punctuation token">]</SPAN> baseUrl <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>{}:6080/arcgis/admin/services"</SPAN></SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>server<SPAN class="punctuation token">)</SPAN> catalog <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>load<SPAN class="punctuation token">(</SPAN>urllib2<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>baseUrl <SPAN class="operator token">+</SPAN> <SPAN class="string token">"/"</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"?token="</SPAN> <SPAN class="operator token">+</SPAN> token <SPAN class="operator token">+</SPAN> <SPAN class="string token">"&f=json"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ROOT"</SPAN><SPAN class="punctuation token">)</SPAN> services <SPAN class="operator token">=</SPAN> catalog<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'services'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> service <SPAN class="keyword token">in</SPAN> services<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> service<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'type'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">!=</SPAN> <SPAN class="string token">'StreamServer'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\t"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>service<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'serviceName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> serviceURL <SPAN class="operator token">=</SPAN> <SPAN class="string token">"{0}/{1}.{2}?token={3}&f=json"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>baseUrl<SPAN class="punctuation token">,</SPAN> service<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'serviceName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> service<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'type'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">)</SPAN> serviceProperties <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>load<SPAN class="punctuation token">(</SPAN>urllib2<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>serviceURL<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\t\t"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>serviceProperties<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> folders <SPAN class="operator token">=</SPAN> catalog<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'folders'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> folderName <SPAN class="keyword token">in</SPAN> folders<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> str<SPAN class="punctuation token">(</SPAN>folderName<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'System'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Utilities'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'DataStoreCatalogs'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>folderName<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> catalog <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>load<SPAN class="punctuation token">(</SPAN>urllib2<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>baseUrl <SPAN class="operator token">+</SPAN> <SPAN class="string token">"/"</SPAN> <SPAN class="operator token">+</SPAN> folderName <SPAN class="operator token">+</SPAN> <SPAN class="string token">"/"</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"?token="</SPAN> <SPAN class="operator token">+</SPAN> token <SPAN class="operator token">+</SPAN> <SPAN class="string token">"&f=json"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> services <SPAN class="operator token">=</SPAN> catalog<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'services'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> service <SPAN class="keyword token">in</SPAN> services<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\t"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>service<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'serviceName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> serviceURL <SPAN class="operator token">=</SPAN> <SPAN class="string token">"{0}/{1}/{2}.{3}?token={4}&f=json"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>baseUrl<SPAN class="punctuation token">,</SPAN> folderName<SPAN class="punctuation token">,</SPAN> service<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'serviceName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> service<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'type'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">)</SPAN> serviceProperties <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>load<SPAN class="punctuation token">(</SPAN>urllib2<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>serviceURL<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\t\t"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>serviceProperties<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>
Thanks Jake.
Hi Jake.
This code is working with sample arcgis online services and posting the results. When trying with my server's site,it fetches the token and posts
error inline 19: services = catalog['services']KeyError: 'services'.What cud be the fix!
Under line 18, add the line:
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>catalog<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
What is returned?
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.