Network Link/ Python script

409 views
Skip to first unread message

Nif

unread,
Apr 9, 2008, 3:15:13 PM4/9/08
to KML Developer Support - KML Server Side Scripting
Howdy -

I'm feeling a)pretty stupid and b)like breaking something.

So I am trying to figure out how to run a simple python script via a
KML network link - from the KML tutorial (here:
http://code.google.com/apis/kml/documentation/kml_tut.html#network_links)

I'm not going to upload the files - you can just cut and paste from
the tutorial.

And I keep getting errors from Google Earth like 1) HTTP server error
or 2) HTTP file not found.

My web server host says it's a "scripting thing" and they will not
help.

I did glean from them the following info:
1) Python scripts, can in theory, be put anywhere, but perl and php
scripts need to be put in a cgi-bin folder. Since the python script I
was originally playing with is functioning internally as a cgi, I'll
stick with this folder for the moment until I understand what is going
on.
2) #!/usr/local/bin/python - this, in theory, is where my python stuff
is located on their servers and is the first line in my python file.
3) with cpanel or maybe it's just my hosting company - there are two
different paths and not sure which to use
a) mysite.com/public_html/cgi-bin/test.py (this is the actual
path)
b) mysite.com/cgi-bin/test.py (this is what gets typed into the
address bar)

So, I think to all of you out there who have been doing this stuff for
ages it might seem basic, but I am really trying to learn server side
stuff and well, it's confusing.

Thanks for any help I can get!

Nif
http://www.steady-as-she-goes.com
http://www.crankybunny.com



mwill

unread,
Apr 9, 2008, 8:50:24 PM4/9/08
to KML Developer Support - KML Server Side Scripting

Nif,

One thing I notice that you didn't include in your post was the
information from these couple sentences:

"Two things are necessary for delivering KML through a network CGI:

When a call is made from the client (Google Earth) to the server, the
server must (1) return a response code of HTTP 200 and (2) set the
response's content type to text/plain or application/vnd.google-
earth.kml+xml."

That could very well cause the problems you are having.

mwill

Nif

unread,
Apr 10, 2008, 10:14:49 AM4/10/08
to KML Developer Support - KML Server Side Scripting
Hi mwill -

Yep - all part of the process of trying to figure out what I need to
know.

I think those are a big part of my lack of understanding and when I
asked the host provider about them (I cut an pasted that exact little
bit :) ) they told me to, in so many words, "not our problem"

My problem is that I don't know what either of those mean or how to
verify them.

How do you check to see that the server is returning a response code
of HTTP 200?
How do you set the response's content type? I tried adding those line
in the python code as I had seen mentioned somewhere on the web. I
tried both types. Is this something that I have to set up with my
hosting company?

Thanks!

It's all part of the learning process.

ManoM

unread,
Apr 10, 2008, 2:48:15 PM4/10/08
to KML Developer Support - KML Server Side Scripting
Hi Nif,

It sounds to me like the problem is in the server configuration. Are
you sure, for instance, that Python is installed on the server? If it
is, you'll need information from the hosting service about where to
put the files.

Mano

Nif

unread,
Apr 10, 2008, 3:19:22 PM4/10/08
to KML Developer Support - KML Server Side Scripting
Howdy -

Yep - python is installed and they gave the path: #!/usr/local/bin/
python

And they said that python scripts can go anywhere but perl/php need to
go in the already setup "cgi-bin" folder. Since the python script I
was trying to work with initially was calling cgi I stuck it in that
folder (cgi-bin) just to be sure - same problems. And I've been
trying the Google tutorial file in the cgi-bin folder just for kicks.

So I guess it's back to - how do I verify/do the two things that are
required by Google Earth?

1 How do you check to see that the server is returning a response code
of HTTP 200?
2) How do you set the response's content type? I tried adding those
line
in the python code as I had seen mentioned somewhere on the web. I
tried both types. Is this something that I have to set up with my
hosting company?

I can't shake the feeling that this is something really simple like
editing some sort of config file on my web server or something like
that.

Nif

ManoM

unread,
Apr 10, 2008, 3:46:58 PM4/10/08
to KML Developer Support - KML Server Side Scripting
Hi Nif,

A couple of things to try:
Do you have access to server logs? If you do, check the error logs and
see if your script is producing any errors. Often hosting providers
will give access to this information.
Try opening the script in a web browser. What do you get?

If those two don't help you figure it out, can you point us to the
script location?

Mano

Nif

unread,
Apr 10, 2008, 3:59:02 PM4/10/08
to KML Developer Support - KML Server Side Scripting
Hi Mano -

OK - the server log...makes not much sense to me. This was the last
entry (and the one's before look much like this)

69.22.240.246 - - [06/Apr/2008:10:57:52 -0500] "GET /xml-api/cpanel
HTTP/1.1" 302 215 "http://cpanel.northarrowguides.com/frontend/x3/
telnet/index.html" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:
1.8.1.13) Gecko/20080311 Firefox/2.0.0.13"

And when I just try to run the script:
******************
Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator,
webm...@northarrowguides.com and inform them of the time the error
occurred, and anything you might have done that may have caused the
error.
More information about this error may be available in the server error
log.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.8b mod_auth_passthrough/
2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at www.northarrowguides.com
Port 80
******************

I don't like cpanel, but that is a different story.

The script - which is a copy of the script on the KML tutorial page
(link in first post) with a single change due to the location of
python on the web server - is...

http://www.northarrowguides.com/cgi-bin/nl_ge_tut.py

Thanks!

Nif

unread,
Apr 10, 2008, 4:00:56 PM4/10/08
to KML Developer Support - KML Server Side Scripting
Oh -

and my .htaccess has this in it...

AddType application/vnd.google-earth.kml+xml .kml
AddType application/vnd.google-earth.kmz .kmz

Nif

ManoM

unread,
Apr 10, 2008, 4:21:03 PM4/10/08
to KML Developer Support - KML Server Side Scripting
Hi Nif,

I'm also getting these errors when i try to access it. Have you tried
just putting it in the htdocs directory and calling ti from
http://www.northarrowguides.com/nl_ge_tut.py ?

Mano

Mano

Nif

unread,
Apr 10, 2008, 4:36:55 PM4/10/08
to KML Developer Support - KML Server Side Scripting
Mano -

Don't have a htdocs directory, but will put it under the "public_html"
folder that is the gateway for access.

This is what I get when I run the kml in Google Earth...

"Fetch of NetworkLink "Random Placemark" failed (http://
northarrowguides.com/nl_ge_tut.py):
Parse error at line1 column 1:
not well-formed (invalitoken )
"

This is the KML text:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Folder>
<name>Network Links</name>
<visibility>0</visibility>
<open>0</open>
<description>Network link example 1</description>
<NetworkLink>
<name>Random Placemark</name>
<visibility>0</visibility>
<open>0</open>
<description>A simple server-side script that generates a new
random
placemark on each call</description>
<refreshVisibility>0</refreshVisibility>
<flyToView>0</flyToView>
<Link>
<href>http://northarrowguides.com/nl_ge_tut.py</href>
</Link>
</NetworkLink>
</Folder>
</kml>


And this is the python script it is calling:

#!/usr/local/bin/python

import random

latitude = random.randrange(-90, 90)
longitude = random.randrange(-180, 180)
kml = (
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<kml xmlns="http://earth.google.com/kml/2.2">\n'
'<Placemark>\n'
'<name>Random Placemark</name>\n'
'<Point>\n'
'<coordinates>%d,%d</coordinates>\n'
'</Point>\n'
'</Placemark>\n'
'</kml>'
) %(longitude, latitude)
print 'Content-Type: application/vnd.google-earth.kml+xml\n'
print kml

Thanks.





On Apr 10, 4:21 pm, ManoM wrote:
> Hi Nif,
>

ManoM

unread,
Apr 10, 2008, 4:46:29 PM4/10/08
to KML Developer Support - KML Server Side Scripting
Yes, if you load the script in the browser, you see the script, so
clearly it isn't triggering. Try adding import cgi to the start of the
file, after import random, see if that helps.

Other than that, this is a server configuration issue, I'm not sure
what to tell you.

Nif

unread,
Apr 10, 2008, 5:01:27 PM4/10/08
to KML Developer Support - KML Server Side Scripting
Yup - that was happening way back last week when I first start this -
I could see the script...but it wouldn't work.

So the web hosting says it's a scripting problem, but it really is
most likely some sort of server set up deal.

I believe I will go back to them, show them this thread, and hopefully
they will help me figure out what to do.

Can you suggest something to say to them - maybe I'm asking the
questions incorrectly - you know, using the wrong vocabulary.

How should this really work?
1) Know the location of python on the web server
2) Know where to put scripts (python, perl, php)
3) Know the correct url to reference in the KML file
4) Have the correct MIME (?) settings on the server

And then stuff works?

This is so simple, yet so confusing.

Thanks and I'll let you know.

Nif

ManoM

unread,
Apr 10, 2008, 8:53:06 PM4/10/08
to KML Developer Support - KML Server Side Scripting
Hi Nif,

You've got the right questions, you might also ask what version of
Pyton they are running and see if that might effect things. You might
also try posting to Python lists. Finally, if you're really into
Python, you might try Google App Engine:
http://code.google.com/appengine/

Mano

Dane

unread,
Apr 11, 2008, 12:18:33 PM4/11/08
to KML Developer Support - KML Server Side Scripting
Nif,
I copied your kml script and it worked fine for me, once I changed
the line endings to Unix Line Endings. (Before I did that I would get
a "permature end of script headers error" in my apache logs and the
server would repond with a "500 Internal Server Error"). Are you
perhaps writing and uploading this script from windows? If so, and you
are running the script on an apache linux server then you'll have to
be careful about line endings.

I put it in my cgi bin, calling it nif.py Then opened a terminal
window and typed:
chmod +x nif.py
This makes it able to be executed as a program.
Then I opened the file in a text editor and converted the line
endings to Unix.
Then I went to http://localhost/cgi-bin/nif.py and the server
responded by downloading the KML.

Make sure you get this step working before trying to link to the
python script from a network link.

Also, I modified the script slightly as below:

----

#!/usr/bin/python
import random
import cgi
latitude = random.randrange(-90, 90)
longitude = random.randrange(-180, 180)
kml = (
'''
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Placemark>
<name>Random Placemark</name>
<Point>
<coordinates>%d,%d</coordinates>
</Point>
</Placemark>
</kml>
''') %(longitude, latitude)
print "Content-Type: application/vnd.google-earth.kml+xml"
print kml

----

which gives:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Placemark>
<name>Random Placemark</name>
<Point>
<coordinates>129,-14</coordinates>
</Point>
</Placemark>
</kml>

Cheers,

Dane

Nif

unread,
Apr 11, 2008, 2:49:29 PM4/11/08
to KML Developer Support - KML Server Side Scripting
Dane - you are my hero!!

So there were only two problems going on here - all the rest were red
herrings and it was truly something very simple.

1) My hosting provider gave me the incorrect path to run python - #!/
usr/local/bin/python. For my system the correct line is #!/usr/bin/
python.
2) ALL it was really was the UNIX vs PC file type. All I had to do
was take the file into TextPad and save as a UNIX file.

I did already have the permissions, but had completely forgotten about
the file type stuff - that's what I get for working on a PC all of the
time now!

Thank you, thank you, thank you - now my weekend can progress and I
can start playing with this stuff more seriously now!!
On Apr 11, 12:18 pm, Dane wrote:
> Nif,
> I copied your kml script and it worked fine for me, once I changed
> the line endings to Unix Line Endings. (Before I did that I would get
> a "permature end of script headers error" in my apache logs and the
> server would repond with a "500 Internal Server Error"). Are you
> perhaps writing and uploading this script from windows? If so, and you
> are running the script on an apache linux server then you'll have to
> be careful about line endings.
>
> I put it in my cgi bin, calling it nif.py Then opened a terminal
> window and typed:
> chmod +x nif.py
> This makes it able to be executed as a program.
> Then I opened the file in a text editor and converted the line
> endings to Unix.
> Then I went tohttp://localhost/cgi-bin/nif.pyand the server
> ...
>
> read more »

Dane

unread,
Apr 12, 2008, 1:50:37 AM4/12/08
to KML Developer Support - KML Server Side Scripting
Nif,

Good to hear. It took me a moment to figure out the line ending thing
too. It's always helpful to get familiar with your python interpreter
and python paths too, before trying to run python scripts as cgi
executables. There is a lot that can need tweaking.

As you get into python and kml you might be interested in trying out
featureserver. As well, I commonly use python scripts as cgi to pull
from a postgres/postgis database to create dynamic kml, so thats
another cool route.

Enjoy,

Dane



On Apr 11, 11:49 am, Nif wrote:
> Dane - you are my hero!!
>
> So there were only two problems going on here - all the rest were red
> herrings and it was truly something very simple.
>
> 1) My hosting provider gave me the incorrect path to run python - #!/
> usr/local/bin/python.  For my system the correct line is #!/usr/bin/
> python.
> 2) ALL it was really was the UNIX vs PC file type.  All I had to do
> was take the file into TextPad and save as a UNIX file.
>
> I did already have the permissions, but had completely forgotten about
> the file type stuff - that's what I get for working on a PC all of the
> time now!
>
> Thank you, thank you, thank you - now my weekend can progress and I
> can start playing with this stuff more seriously now!!
>
> Nif
>
> http://www.steady-as-she-goes.comhttp://www.crankybunny.com
>
> On Apr 11, 12:18 pm, Dane wrote:
>
> > Nif,
> >  I copied your kml script and it worked fine for me, once I changed
> > the line endings to Unix Line Endings. (Before I did that I would get
> > a "permature end of script headers error" in my apache logs and the
> > server would repond with a "500 Internal Server Error"). Are you
> > perhaps writing and uploading this script from windows? If so, and you
> > are running the script on an apache linux server then you'll have to
> > be careful about line endings.
>
> >  I put it in my cgi bin, calling it nif.py Then opened a terminal
> > window and typed:
> >  chmod +x nif.py
> >  This makes it able to be executed as a program.
> >  Then I opened the file in a text editor and converted the line
> > endings to Unix.
> >  Then I went tohttp://localhost/cgi-bin/nif.pyandthe server
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages