import arcpy fails with cgi script

1219
2
08-19-2013 02:45 PM
JamesGustine
New Contributor III
Hello,

I'm looking to get this simple map export to run via an url and am running into issues when importing arcpy. It runs fine locally, but gives a 502-webserver recieved and invalid response while acting as a gateway or proxy server when run from another computer.

import cgitb, sys, string, os, arcpy, cgi
cgitb.enable()

print "Content-Type: text/plain;charset=utf-8"
print
#print ""
form = cgi.FieldStorage()
arg = form.getvalue('agn')

#tMapDir = tMapDir
tMapDir = r"C:\inetpub\wwwroot\cgi-bin\Map.pdf"

arcpy.env.overwriteOutput = True

# Get Parameters
agn = arg

# Set Map
mapDoc = arcpy.mapping.MapDocument(r"C:\inetpub\wwwroot\cgi-bin\ContractReport.mxd")
df = arcpy.mapping.ListDataFrames(mapDoc)[0]
lyr = arcpy.mapping.ListLayers(mapDoc, r"ROWs, CAs & Surface Leases", df)[0]


dq = "(INVOICENUMBER = '" + agn + "'" + ")"
lyr.definitionQuery = dq
df.extent = lyr.getExtent()

for elm in arcpy.mapping.ListLayoutElements(mapDoc, "TEXT_ELEMENT"):
    if elm.name == "INVOICENUMBER":
        elm.text = agn

arcpy.mapping.ExportToPDF(mapDoc, tMapDir, resolution="300", image_quality="NORMAL", layers_attributes="NONE", georef_info=False, jpeg_compression_quality="80")

#Set output file


URL = "http://localhost/cgi-bin/Map.pdf"

print "Content-type:text/html\r\n\r\n"
print '<html> '
print '<head> '
print '<meta http-equiv="refresh" content="0;url='
print URL
print '" /> '
print '<title>Request Submitted, Redirecting...</title> '
print '</head> '
print '<body> '
print 'Loading map...'
print URL
print '</body> '
print '</html>'

lyr.definitionQuery = ""

del mapDoc

sys.exit()


The problem is definetly with the import arcpy as I can run a simple helloworld.py with standard py library imports (os, sys, cgi, string, stc) and it is accessbile from other machines.

I have a simple default site set up on my computer using IIS 7.

Any thoughts on getting arcpy to import for cgi scripts from other computers? I know importing arcpy checks out a license and does wires up arcobjects. Is accesssing the module from a URL even possible?

Thanks,

James
Tags (2)
0 Kudos
2 Replies
ColletonGISAdmin
New Contributor III
I am having the same issue. At 10.0, I did successfully integrate Python CGIs using arcpy and implemented a number of applications with that approach. We're migrating to 10.1 and concurrently I'm trying to update my CGI scripts, but they've been failing too, with a 502 error. However, when I swap out my cgiFieldStorage() variables with hardcoded variables, the scripts run\output as they should.

If you get an update or solution regarding this issue, I'd love to hear about it.

Thanks,
Eric
0 Kudos
JohnReiser
New Contributor III
Your problem is likely due to a CGI script starting from scratch for each request & IIS is timing out because the script takes too long to start. You might want to look into a framework like FastCGI or Web.py. That way, your ArcGIS Engine (via arcpy) is fired up once and left running until the server shuts down. Each request of the FastCGI/Web.py script reuses the same objects from modules like arcpy, psycopg2, etc instead of recreating them each time.

FastCGI on IIS: http://www.iis.net/downloads/microsoft/fastcgi-for-iis
Web.py: http://webpy.org/ (on IIS: http://webpy.org/cookbook/iis7_iis6_windows_pyisapie)
0 Kudos