HOW TO CHECK MODULES INSTALLED IN PYTHON


If there is no PIP, install PIP on the python folder by:

Python.exe get-pip.py

Once pip installed, run:

installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted([“%s==%s” % (i.key, i.version)
for i in installed_packages])
print(installed_packages_list)

or you can run, from python interpreter

help(‘modules’)

or create this into modules.py

import sys
import pydoc

def output_help_to_file(filepath, modul):
f = file(filepath, ‘w’)
sys.stdout = f
pydoc.help(modul)
f.close()
sys.stdout = sys.__stdout__
return

output_help_to_file(r’modules.txt’, ‘modules’)
To check Python version:
1. Search for any python.exe on machine
2. From python interpreter,
Import sys
Sys.version_info

Print(sys.prefix)

Download Attachments from Microsoft Access using Python


Have a ton of attachments in your Access Database, Do you want to download all attachments from Microsoft Access Database using script?


import win32com.client
import os

# Log file location
log_file = r"c:\Users\mlou\Documents\Projects\convert_log1.txt"
working_path = r"c:\Users\mlou\Documents\Projects\Images"

doc = open(log_file, 'w+')

daoEngine = win32com.client.Dispatch('DAO.DBEngine.120')
db = r"C:\Users\mlou\Documents\Projects\AccessDB_12_14_2018.accdb"
daoDB = daoEngine.OpenDatabase(db)

# Do query to database
query = "SELECT * FROM Plants"

daoRS = daoDB.OpenRecordset(query,2)
while not daoRS.EOF:

      print ("Field ID "+ str(daoRS.Fields["ID"].Value) +"\n")

      doc.write("Field ID "+ str(daoRS.Fields["ID"].Value) +"\n")
      daoAttachmentRS = daoRS.Fields["Photograph"].Value

      while not daoAttachmentRS.EOF:
           new_path = working_path+"\\"+str(daoRS.Fields["ID"].Value)
           if not os.path.exists(new_path):
               os.mkdir(new_path)
           rec_count = daoAttachmentRS.RecordCount
           if daoAttachmentRS.RecordCount <> 0:
                 re_field = daoAttachmentRS.Fields["FileData"]

                 daoAttachmentRS.Fields["FileData"].SaveToFile(new_path)
       daoAttachmentRS.MoveNext()

daoRS.MoveNext()
daoDB.Close()
doc.close()

Error: The content directory has no items directory


Got this error when you want to add second Portal site on ArcGIS 10.5.1? Try this:

Update the Portal Directory from local source to shared directory and make sure to change \ (slash) with / (backslash). Example:

{
“type”:”fileStore”,
“provider”:”FileSystem”,
“connectionString”:”//server01/prod/Portal_Content”,
“isConnectionStringEncrypted”:false
}

 

Hope this works. Good Luck!!!!

001487: Failed to update the published service with the server-side data location. Please see the server’s log for more details


Ho ho ho…getting this error when trying to publish a map? After upgrade? I got this error after upgrade from 10.3.1 and 10.4.1. I made several calls to Esri support, no help at all, they were thinking about file permission. I said what? How come file permission got change after upgrade? It doesn’t make sense and I said the account for ArcGIS server service is member of Administrator group and should have full permission to any files/folders.

Anyway…let me give you some background. After upgrade, I tried to publish a simple map that using Oracle database, so it means I used registered database from data store, it kept fail and threw the error. I checked the server log and I found this:

Failed swizzling the service.

Then

Failed to rename cache folder.

Then

Failed to create the service.: Updating the server connection string for layer Entries failed. Attempted connection string was ENCRYPTED_PASSWORD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;SERVER=sde;INSTANCE=”DSID=ffffffffffffffffffffffff”;DBCLIENT=oracle;DB_CONNECTION_PROPERTIES=TEST;PROJECT_INSTANCE=sde;USER=ARCSDE;VERSION=SDE.DEFAULT;AUTHENTICATION_MODE=DBMS. Table name is ARCSDE.COUNTIES. Please verify the data exists on the server.

Wow..first..what is swizzling? like mixing drink? And the last error message that made me puzzling, what? Verify the data exists on the server?

I did run Validate All on Server Manager, all showed green checked and did the same from ArcCatalog, all showed green as well. So it meant both from server and from local, it could connect and verify the database connection. So what is wrong then?

As I mentioned in the beginning, I called Esri support and I did my own search on Google. I found some interesting info.

  1. Bug – what? bug? Really?, here is the link, http://support.esri.com/download/7397 . Then it would be easy for me, just apply the patch and will work. Apparently no…the patch didn’t fix the issue.
  2. Found something in ArcGIS Pro reference, http://pro.arcgis.com/en/pro-app/tool-reference/tool-errors-and-warnings/001001-010000/tool-errors-and-warnings-01476-01500-001487.htm . Well…it didn’t make sense for my case.
  3. Found this thread, https://geonet.esri.com/thread/173653 … Well…very promising…until someone named, “Buddhatown”, gave a link
  4. Here is the link, https://geonet.esri.com/thread/177553#comment-614422 . Some gave steps on how to publish a service “manually” which is create MSD file and then publish from Admin site.
  5. Finally, inside that thread, someone named “twheagle-co-nz-esridist” gave this tip..THIS IS REAL HELP. Exactly the same as my case.

 

Let me give you my tips on how to fix:

  1. Check if your …\config-store\data has dsconnections.lst file or not, if not, continue below otherwise, please stop, it doesn’t apply to your caseJ.
  2. Find other site that working fine, has the same connection/registered database, copy the whole \data folder to the broken site AND copy dsconnections.lst file to all servers that member of the site in this location …\ArcGIS\Server\DatabaseSupport. REMEMBER: when you do that, you must stop ArcGIS server service on both source and target.
  3. If you can’t find other site that working fine, if you have multiple servers in a site, remove all servers and left only one server in the site (the idea is to make only one server/site), on Server Manager go to Site – Data Store, remove all Registered Databases entries and Registered Folders entries. Re-entry Register Database first until finish and continue to Register Folder. Make sure when you register first database, check if it created dsconnections.lst file on config-store\data folder. Otherwise, you might need to restart the server.

Those tips that I mentioned, fixed my issue. Thank you very much to those people in the Esri forum threads that give information and idea.

Meanwhile, I have some questions to Esri folks if might get chance to read my post,

  1. Why created dsconnections.lst file on both config-store and each server? I saw there were some inconsistency such as if I have multiple servers in a site, not all servers have identical file compare to each other and compare to config-store.
  2. Why created dsconnections.lst file on local if you are going to use the file on config-store folder?

Suggestion: if anytime we run validation using ArcCatalog and the result is green, why not copy the file from the local to config-store folder and to all servers member of the site (synchronization) and the other way from server manager. By that, all servers in the site and config-store folder will have identical file. Or to make it simple, why maintain critical configuration of a site in local server while you have a central location \config-store and \directories.

 

GOOD LUCK!!!

How to Install XGBoost on Windows


When you googled and landed to this blog, I could say you are struggling to install XGBoost on your Python. Anyway, My environment is Windows 7 and I use Spyder Python 3.4.4. I like this Spyder, it has so many modules available such as Panda, Numpy, Seaborn, Matplotlib, etc but no XGBoost. Let’s me give you some tips on how to install it:

  1. Go to this GitHub, https://github.com/dmlc/xgboost and download the folder python-package. Save it into your python environment. …\python-package\xgboost. it depends on where you have your python.exe. I have several python versions on my computer.
  2. Go to this site, http://www.picnet.com.au/blogs/guido/post/2016/09/22/xgboost-windows-x64-binaries-for-download/ and download  the latest libxgboost.dll. And save it to the same location as number 1.
  3. run python setup.py install.
  4. Once finished, open python console and type import xgboost to check if your installation ran successfully or not.

 

Good Luck!!!!!

How many time did your service been hit?


One day some one, could be your manager, customer or some one else, asked you if you could give them metric on how many time in a month or week your services have been hit. There are many ways to answer this and many tools available out there. Here, I would like to give you a free tool or you could say it as script where you can run and give an idea to your requester how many hits on your ArcGIS services.You can also modify this script to not only produce a csv file but also input into database, so everyone can access the data.

 

Good Luck,

 


#-------------------------------------------------------------------------------
# Name: HitStat.py
# Purpose: Gather hit statistics info in the last 24 hours. the statistics will be stored in DB and csv file
# Since there is limitation in Esri side how it pulls the data to max. 10,000 records from log file,
# We divide to an hour in 24 hours time frame. Even though each our has max 10,000 records at least
# it can show a close accurate data. The steps are: Pul each hour data into a temp csv and read the
# data from there and store in csv and database.
# Queries the logs and writes statistics on map service activity during the past 24 hours
# Sends email if no log data, indicating someone has changed ArcGIS Server Log settings
# The data is based on CODE = 100004 and 8521
#
# Author: mlou1_p
#
# Created: 04/13/2016
# Copyright: (c) mlou1_p 2016
# Licence: &lt;your licence&gt;
#-------------------------------------------------------------------------------

# For Http calls
import httplib, urllib, json

# CSV Module
import csv

# Datetime module
import datetime

# For system tools
#import arcserver
import sys, time, arcserver, arcpy, re

# For sending email
import smtplib
import mimetypes
import email
import email.mime.application

# For reading passwords without echoing
import getpass

#Get the current date
now = datetime.datetime.now()

#Defines the entry point into the script
def main(argv=None):
# Print some info
print
print &quot;This tool is a script that queries the ArcGIS Server logs and writes a report&quot;
print &quot; summarizing all map service draws within the past 24 hours.&quot;
print

# ########################################################################################################################################
#--- START OF VARIABLE--------------------------------------------------------------------------------------------------------------------
# ########################################################################################################################################

# Ask for admin/publisher user name and password
#username = raw_input(&quot;Enter user name: &quot;)
username = &quot;ARCGISADMIN&quot;
#password = getpass.getpass(&quot;Enter password: &quot;)
password = &quot;Abc123$&quot;

# Ask for server name
#serverName = raw_input(&quot;Enter Server name: &quot;)
serverName = &quot;ARCGISSERVER01&quot;
serverPort = 6080

# Connect to Oracle to put the data
##tbl = r&quot;\\stlArcGISsvr\GIS_Data\data\ADM\GEOSPATIAL@GEORACLE.sde\GEOSPATIAL.HIT_STATISTICS&quot; #turn off for testing

# Ask for text file path
filePath = &quot;//stlArcGISsvr//d$//temp//HitStat//HitStat_&quot; + str(now.year) + str(now.month) + str(now.day) + &quot;.txt&quot;
rawPath = &quot;//stlArcGISsvr//d$//temp//HitStat//raw.csv&quot;

# For email purposes
From = &quot;noreply@esri.com&quot;
Receivers = [&quot;DL-DOOOH@esri.com&quot;] # must be a list
EmailBody = &quot;No Draw Time Log for DEV environment. Check ArcGIS Server Log Settings.&quot;

# For Testing Purpose please use this to look for specific date
#Date time start with
date_time = '04.12.2016 00:00:01'
pattern = '%m.%d.%Y %H:%M:%S'
##startTime = int(time.mktime(time.strptime(date_time, pattern))*1000)

startTime = int(round(time.time() * 1000)) # Current time and date

#millisecondsToQuery = 86400000 # One day
##millisecondsToQuery = 3600000 # One hour in millisecond
##n = 24 #1 hours X 24 = a day
millisecondsToQuery = 1800000 # half an hour in millisecond (30 minutes)
n = 48 # 30 min X 48 = a day

# ########################################################################################################################################
#--- END OF VARIABLE--------------------------------------------------------------------------------------------------------------------
# ########################################################################################################################################

hitDict = {}
counter = 0
timeStamp = '%04d' % now.year + '%02d' % now.month + '%02d' % now.day

# Open text file and write header line
summaryFile = open(filePath, &quot;w&quot;)
header = &quot;Now;Service;Number of hits\n&quot;

#Raw Data
rawData = open(rawPath,&quot;w&quot;)
rawData.write(&quot;Service;Code&quot;+&quot;\n&quot;)

# Get a token
token = getToken(username, password, serverName, serverPort)
if token == &quot;&quot;:
print &quot;Could not generate a token with the username and password provided.&quot;
return

#insert cursor variables in Oracle
##fields = [&quot;TIME&quot;, &quot;SERVICE&quot;, &quot;HITS&quot;] #turn off for testing

#insert cursor
##insCur = arcpy.da.InsertCursor(tbl, fields) # turn off insCur for testing purposes

# Construct URL to query the logs
logQueryURL = &quot;/arcgis/admin/logs/query&quot;

##endTime = startTime - millisecondsToQuery

# Need these variables to calculate average draw time for an ExportMapImage call
mapDraws = 0
totalDrawTime = 0
timecount = 0

while (timecount &lt; n):

if timecount &lt;&gt; 0:
rawData = open(rawPath,&quot;a&quot;)

timecount = timecount + 1
print &quot;time count = &quot; +str(timecount)
xTime = millisecondsToQuery * timecount
#startTime = millisecondsToQuery + endTime
endTime = startTime - millisecondsToQuery
print &quot;xTime = &quot; + str(xTime)
print &quot;Start Time = &quot; + str(startTime)
print &quot;End Time = &quot; + str(endTime)

#logFilter = &quot;{'services':'*','server':'*','machines':'*'}&quot;
logFilter = &quot;{'codes':[100004, 8521],'services':'*','server':'*','machines':'*'}&quot; #Correct Code: 100004, changed to 12345 for test purposes

params = urllib.urlencode({'level': 'FINE', 'startTime': startTime, 'endTime': endTime, 'filter':logFilter, 'token': token, 'pageSize': 10000, 'f': 'json'})
headers = {&quot;Content-type&quot;: &quot;application/x-www-form-urlencoded&quot;, &quot;Accept&quot;: &quot;text/plain&quot;}

# Connect to URL and post parameters
httpConn = httplib.HTTPConnection(serverName, serverPort)
httpConn.request(&quot;POST&quot;, logQueryURL, params, headers)

# Read response
response = httpConn.getresponse()
if (response.status != 200):
httpConn.close()
print &quot;Error while querying logs.&quot;
return
else:
data = response.read()

# Check that data returned is not an error object
if not assertJsonSuccess(data):
print &quot;Error returned by operation. &quot; + data
else:
print &quot;Operation completed successfully at &quot; + str(timecount) + &quot; hour&quot;

# Deserialize response into Python object
dataObj = json.loads(data)
httpConn.close()

# Iterate over messages
for item in dataObj[&quot;logMessages&quot;]:

# if item[&quot;message&quot;] == &quot;End ExportMapImage&quot;:
##elapsed = float(item[&quot;elapsed&quot;])
keyCheck = item[&quot;source&quot;]
code = str(item[&quot;code&quot;])

msg = item[&quot;message&quot;]
##print &quot;Ori msg : &quot; + msg

# If Code is 100004
if '8521' not in code:
rawData.write(keyCheck+ &quot;;&quot; + str(code) + &quot;\n&quot;)
print &quot;KeyCheck = &quot; +keyCheck

#Input the result into temporary dictionary (hitDict)
if keyCheck in hitDict:
stats = hitDict[keyCheck]

# Add 1 to tally of hits
stats[0] += 1

else:
# Add key with one hit and total elapsed time
hitDict[keyCheck] = [1]

# If Code is 8521
else:
if 'services/' in msg:
if 'Server' in msg:

# Trim/Slice data to get Service Name

astrVerify = &quot;arcgis/rest/services/&quot;
a = msg.find(astrVerify)
msg = msg[a+len(astrVerify):]

bstrVerify = &quot;Server&quot;
b = msg.find(bstrVerify)
msg = msg[0:b+6]

# Replace the message that has second / with .
msg = re.sub(r'^((.*?/.*?){1})/', r'\1.', msg)

rawData.write(msg + &quot;;&quot; + str(code) + &quot;\n&quot;)
print &quot;KeyCheck = &quot; +msg
keyCheck = msg

#Input the result into temporary dictionary (hitDict)
if keyCheck in hitDict:
stats = hitDict[keyCheck]

# Add 1 to tally of hits
stats[0] += 1

else:
# Add key with one hit and total elapsed time
hitDict[keyCheck] = [1]

startTime = endTime
rawData.close()

## #If no data, send email to GEOSPATIAL
## if hitDict == {}:
## EmailSubject = &quot;No Draw Times found in log.&quot;
## for address in Receivers:
## EmailAttachments(address, From, EmailSubject, EmailBody)
##
## print &quot;no draw times found!&quot;

#Read the Raw Data
# Open text file and write header line
summaryFile = open(filePath, &quot;w&quot;)
header = &quot;Now;Service;Number of hits\n&quot;
summaryFile.write(header)
print hitDict

# Read through dictionary and write totals into file
for key in sorted(hitDict.iterkeys()):

# Calculate average elapsed time
totalDraws = hitDict[key][0]

# Construct and write the comma-separated line
line = timeStamp +&quot;,&quot; + key + &quot;,&quot; + str(totalDraws) + &quot;\n&quot;

fields = [&quot;TIME&quot;, &quot;SERVICE&quot;, &quot;HITS&quot;]
##insCur.insertRow(( timeStamp, key, str(totalDraws) )) # comment out for testing

summaryFile.write(line)

summaryFile.close()
return

# ########################################################################################################################################
#--- DEFINED ALL FUNCTIONS----------------------------------------------------------------------------------------------------------------
# ########################################################################################################################################

# A function to generate a token given username, password and the adminURL.
def getToken(username, password, serverName, serverPort):
# Token URL is typically http://server[:port]/arcgis/admin/generateToken
tokenURL = &quot;/arcgis/admin/generateToken&quot;

# URL-encode the token parameters
params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})

headers = {&quot;Content-type&quot;: &quot;application/x-www-form-urlencoded&quot;, &quot;Accept&quot;: &quot;text/plain&quot;}

# Connect to URL and post parameters
httpConn = httplib.HTTPConnection(serverName, serverPort)
httpConn.request(&quot;POST&quot;, tokenURL, params, headers)

# Read response
response = httpConn.getresponse()
if (response.status != 200):
httpConn.close()
print &quot;Error while fetching tokens from admin URL. Please check the URL and try again.&quot;
return
else:
data = response.read()
httpConn.close()

# Check that data returned is not an error object
if not assertJsonSuccess(data):
return

# Extract the toke from it
token = json.loads(data)
return token['token']

#A function that checks that the input JSON object
# is not an error object.
def assertJsonSuccess(data):
obj = json.loads(data)
if 'status' in obj and obj['status'] == &quot;error&quot;:
print &quot;Error: JSON object returns an error. &quot; + str(obj)
return False
else:
return True

# A function to send email
def EmailAttachments(address, From, EmailSubject, EmailBody):
sender = From
receiver = address

# Create a text/plain message
msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = EmailSubject
msg['From'] = sender
msg['To'] = receiver

# The main body is just another attachment
body = email.mime.Text.MIMEText(EmailBody)
msg.attach(body)

# Send the message via our own SMTP server, but don't include the
# envelope header.
smtpObj = smtplib.SMTP('mail.monica.com')
smtpObj.sendmail(sender, address, msg.as_string())
smtpObj.quit()

# Script start
if __name__ == &quot;__main__&quot;:
sys.exit(main(sys.argv[1:]))&lt;/pre&gt;
&lt;pre&gt;

The Headache of Password Caching in MXD file


Got headache of password caching in your mxd files?

If your company have policy to change account password frequently and has maximum bad password and your mxd document is getting data from an account that affect to it, most likely you will have problem locking the account. I have been researching the behavior of mxd document using a tool and found:

  • It caches password up to 3 times
  • It will cache password forever if you deleted a layer as the layer information still exists in the file, take a look this screenshot. This layer called Layer A After, it still exists in the file even I couldn’t see it from ArcMapHex

 

How to fix it?

Good question! There is ONLY one way that I know so far…it is…create a new mxd file! Or inject the encrypted password with latest one. Any comments?

A Request from one of my blog readers


Folks,

Some of you might come to my blog after googling around and as part of my mission to publish my notes is to help and share. Below here is a request came from Sepideh who is asking some of you or all of you via my blog to support his effort in creating a new Site on StackExchange.

Thank you.

 

 

Dear Muryadi,
As you might have already noticed, there aren’t much places on the net which professionally concentrate on remote sensing and photogrammetry and gather related researchers all over the world to exchange knowledge and improve the community. So I’ve proposed to build a new Q&A site for remote sensing and photogrammetry which is possible through the Area51 zone of StackExchange. (For further information, you may refer to the following links: FAQ and QA site.pdf)
In order to convince the stackexchange management team about the necessity of creating such a site, I need the support of the community. So regarding the close relationship between GIS and remote sensing would you please promote the idea on your website and ask your user to follow this link?
Remote Sensing and Photogrammetry Q&A site proposal
As you can see in the right hand side of the above page, we need 23 more questions with a score of 10 or more to jump to the next phase of the site creation process. Therefore, it would be appreciated if you could tell your users to take some minutes to judge the proposed questions by voting them up and down and commenting on them based on their relevance to the field of remote sensing and photogrammetry. (If you wish to upvote, please upvote the questions that have less than 10 scores not the ones that have been upvotted more than 10 times. At this step upvoting under 10 score questions is even more important than following. However, following is still necessary)
Also could you please ask them to add at least one expert question to the list if you have enough time? (This is needed to decide about the content of the future site)
King Regards,
Links:
Area51 zone : http://goo.gl/yYmjAv
StackExchange : http://goo.gl/MUwsne
FAQ : http://goo.gl/B5BeLa
QA site.pdf : https://goo.gl/br2Pm3
stackexchange management team :http://goo.gl/bVsbyD
Remote Sensing and Photogrammetry Q&A site proposal : http://goo.gl/z3jRfN
—————————————————————-
Sepideh Abadpour
M.Sc. Student in Remote Sensing
University of Tehran, College of Engineering,
School of Surveying and Geospatial Engineering
Email Address1: sepidehabadpour@ut.ac.ir
Email Address2: abadpours@gmail.com

Collect All Service Properties Information in Site


You can find your service properties information from ArcGIS server manager or admin but how about if you want to see all of your services’ properties? you can run a script and see from there. I found this script was really useful when i wanted to check if any publisher published their service without accepting default value, for example: change minimum instances or maximum instances or maximum idle time or different cluster. Or with a little modification, you can query some other information.

Hopefully you find this script is useful for you.

Cheers…

 

#-------------------------------------------------------------------------------
# Name: List_Svc_properties.py
# Purpose: List All Started Services and their properties
#
# This script could be scheduled to run at a regular interval.
#
# Author: Muryadi Oey
#
# Created: 03/11/2016
# Copyright: (c) muryadioey 2016
# Licence: &lt;your licence&gt;
#-------------------------------------------------------------------------------

# For Http calls
import httplib, urllib,json,urllib2

# For system tools
import sys, datetime

# For reading passwords without echoing
import getpass

#Get the current date
now = datetime.datetime.now()

# Defines the entry point into the script
def main(argv=None):

# ##########################################################################################################################
# Start of Variables
# ##########################################################################################################################

# Server name
##serverName = raw_input(&quot;Enter server name: &quot;)
serverName = 'SERVER01'

# Admin/publisher user name and password
## username = raw_input(&quot;Enter user name: &quot;)
## password = getpass.getpass(&quot;Enter password: &quot;)
username = 'ARCGISADMIN'
password = 'passwordadmin'

# Log file location
filePath = r'\\server01\d$\temp\Svc_Properties.txt'
doc = open(filePath, 'w')
header = &quot;Server;Service;Status;MinIns;MaxIns;MaxIdle;MaxStart;MaxUsage;MaxWait;IsolationLevel;Cluster&quot;
doc.write(header +&quot;\n&quot;)

# ###########################################################################################################################
# End of Variables
# ###########################################################################################################################
# Print some info
print
print &quot;This script is to collect all services' information.&quot;
serverPort = 6080
print

# Create a list to hold folder names
folderList = []

# Create a list to hold stopped/started services
targetList = []

# Get a token
token = getToken(username, password, serverName, serverPort,doc)
if token is None:
print &quot;Could not generate a token with the username and password provided.&quot;
doc.write(&quot;\nCould not generate a token with the username and password provided.&quot; +&quot;\n&quot;)
doc.close()
return

folderList = getCatalog(token,serverName,serverPort,doc)

for folder in folderList:
count = targetList.__len__()
i =0
folderName = folder
print &quot;Processing folder : &quot; + folderName
#doc.write(&quot;\n&quot;+ &quot;Processing folder : &quot; + folderName + &quot;\n&quot;)
while(i &lt; count):
targetList.remove(targetList.__getitem__(0))
i = i +1

# Construct URL to read folder
if folder == &quot;ROOT&quot;:
folder = &quot;&quot;
else:
folder += &quot;/&quot;

folderURL = &quot;/arcgis/admin/services/&quot; + folder

# This request only needs the token and the response formatting parameter
params = urllib.urlencode({'token': token, 'f': 'json'})

headers = {&quot;Content-type&quot;: &quot;application/x-www-form-urlencoded&quot;, &quot;Accept&quot;: &quot;text/plain&quot;}

# Connect to URL and post parameters
httpConn = httplib.HTTPConnection(serverName, serverPort)
httpConn.request(&quot;POST&quot;, folderURL, params, headers)

# Read response
response = httpConn.getresponse()
if (response.status != 200):
httpConn.close()
print &quot;Could not read folder information.&quot;
doc.write(&quot;\n&quot; + &quot;Could not read folder information.&quot; +&quot;\n&quot;)
return
else:
data = response.read()

# Check that data returned is not an error object
if not assertJsonSuccess(data,doc):
print &quot;Error when reading folder information. &quot; + str(data)
#doc.write(&quot;\n&quot; + &quot;Error when reading folder information. &quot; + str(data)+&quot;\n&quot;)
else:
print &quot;Processed folder information successfully. Now processing services...&quot;
#doc.write(&quot;Processed folder information successfully. Now processing services...&quot; +&quot;\n&quot;)
print

# Deserialize response into Python object
dataObj = json.loads(data)
httpConn.close()

counter = 0

# Loop through each service in the folder and stop or start it
for item in dataObj['services']:

fullSvcName = item['serviceName'] + &quot;.&quot; + item['type']

# Construct URL to stop or start service, then make the request
statusURL = &quot;/arcgis/admin/services/&quot; + folder + fullSvcName + &quot;/status&quot;
httpConn.request(&quot;POST&quot;, statusURL, params, headers)
# Read status response
statusResponse = httpConn.getresponse()
if (statusResponse.status != 200):
httpConn.close()
print &quot;Error while checking status for &quot; + fullSvcName
doc.write(&quot;\n&quot; + &quot;Error while checking status for &quot; + fullSvcName +&quot;\n&quot;)
return
else:
statusData = statusResponse.read()

# Check that data returned is not an error object
if not assertJsonSuccess(statusData,doc):
print &quot;Error returned when retrieving status information for &quot; + fullSvcName + &quot;.&quot;
doc.write(&quot;\n&quot; + &quot;Error returned when retrieving status information for &quot; + fullSvcName + &quot;.&quot; +&quot;\n&quot;)
print str(statusData)
doc.write(&quot;\n&quot; + str(statusData) +&quot;\n&quot;)

else:

statusDataObj = json.loads(statusData)
status = statusDataObj['realTimeState']

minInstURL = &quot;/arcgis/admin/services/&quot; + folder + fullSvcName
httpConn.request(&quot;POST&quot;, minInstURL, params, headers)

# Collect all services information, you can also query any information available in Service properties
# Read status response
minInstResponse = httpConn.getresponse()
minInstData = minInstResponse.read()
minInstDataObj = json.loads(minInstData)
minIns = minInstDataObj['minInstancesPerNode']
maxIns = minInstDataObj['maxInstancesPerNode']
maxIdle = minInstDataObj['maxIdleTime']
maxStr = minInstDataObj['maxStartupTime']
maxUsg = minInstDataObj['maxUsageTime']
maxWait = minInstDataObj['maxWaitTime']
isoLvl = minInstDataObj['isolationLevel']
cluster = minInstDataObj['clusterName']
print (serverName + ';'+ str( fullSvcName) + ';'+ str(status) + ';'+ str(minIns) +';' + str(maxIns) +';' +str(maxIdle) +';' +str(maxStr) +';' +str(maxUsg) +';' +str(maxWait) +';' +str(isoLvl) +';' +str(cluster) )
doc.write(serverName + ';'+ str( fullSvcName) + ';'+ str(status) +';'+ str(minIns) +';' + str(maxIns) +';' +str(maxIdle) +';' +str(maxStr) +';' +str(maxUsg) +';' +str(maxWait) +';' +str(isoLvl) +';' +str(cluster) +'\n' )
httpConn.close()

print
doc.close()
return

def getCatalog(token,server,port,doc):
baseUrl = &quot;http://{}:{}/arcgis/admin/services&quot;.format(server, port)
folderNameList = []
catalog = json.load(urllib2.urlopen(baseUrl + &quot;/&quot; + &quot;?f=json&amp;token=&quot; + token))
print ' List of Folders :'
#doc.write(&quot; List of Folders :&quot; +&quot;\n&quot;)
print '\tROOT'
#doc.write(&quot;\tROOT&quot; +&quot;\n&quot;)
folderNameList.append(&quot;ROOT&quot;)
if &quot;error&quot; in catalog: return
folders = catalog['folders']
for folderName in folders:
folderNameList.append(folderName)
print '\t'+folderName
#doc.write(&quot;\t&quot;+folderName +&quot;\n&quot;)
if &quot;error&quot; in catalog: return
else: return folderNameList
# A function to generate a token given username, password and the adminURL.
def getToken(username, password, serverName, serverPort,doc):
# Token URL is typically http://server[:port]/arcgis/admin/generateToken
tokenURL = &quot;/arcgis/admin/generateToken&quot;

params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})

headers = {&quot;Content-type&quot;: &quot;application/x-www-form-urlencoded&quot;, &quot;Accept&quot;: &quot;text/plain&quot;}

# Connect to URL and post parameters
httpConn = httplib.HTTPConnection(serverName, serverPort)
httpConn.request(&quot;POST&quot;, tokenURL, params, headers)

# Read response
response = httpConn.getresponse()
if (response.status != 200):
httpConn.close()
print &quot;Error while fetching tokens from admin URL. Please check the URL and try again.&quot;
doc.write(&quot;\n&quot; + &quot;Error while fetching tokens from admin URL. Please check the URL and try again.&quot; +&quot;\n&quot;)
return
else:
data = response.read()
httpConn.close()

# Check that data returned is not an error object
if not assertJsonSuccess(data,doc):
return

# Extract the token from it
token = json.loads(data)
return token['token']
# A function that checks that the input JSON object
# is not an error object.
def assertJsonSuccess(data,doc):
obj = json.loads(data)
if 'status' in obj and obj['status'] == &quot;error&quot;:
print &quot;Error: JSON object returns an error. &quot; + str(obj)
#doc.write(&quot;\n&quot; + &quot;Error: JSON object returns an error. &quot; + str(obj) +&quot;\n&quot;)
return False
else:
return True
# Script start
if __name__ == &quot;__main__&quot;:
sys.exit(main(sys.argv[1:]))