【发布时间】:2014-02-17 23:05:09
【问题描述】:
我对python和html非常陌生,我一直在尝试使用python(使用cgi)运行一个简单的httpserver。从这台服务器我可以运行一个 location.py 文件来显示用户的地理位置。然后我想把这个位置写到我电脑上的一个文本文件中,但我不知道该怎么做。目前我的 location.py 实际上是一个由 python 加载的 html5 页面,但我不知道如何从我的这个页面中提取所需的信息。
附:这样做的灵感是让我的 iphone 和我的笔记本电脑共享同一个 wifi 网络(adhoc 便携式热点),然后让我的笔记本电脑在谷歌地球中使用我的 iphone 更准确的 gps。这我最终想写入一个 kml 文件,该文件会不断更新我当前的纬度和经度。可能有更简单的方法可以做到这一点,但我认为学习一点 Python 会是个好主意。
我的 location.py 文件
#!/python
print( "Content-type: text/html\n\n")
print( """\
<html>
<head>
<p id="demo">Test:</p>
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
window.onload = getLocation;
</script>
</head>
<body>
</body>
</html>""")
还有我的服务器文件(webserver.py)
import http.server
def main():
server_address = ("", 8000)
handler = http.server.CGIHTTPRequestHandler
handler.cgi_directories = ['/python']
server = http.server.HTTPServer(server_address, handler)
try:
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()
if __name__ == '__main__':
main()
【问题讨论】:
标签: python html python-3.x geolocation