6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonで今いる位置情報やユーザーエージェントなんかを取得

Last updated at Posted at 2015-12-03

まぁ何に使うかはその人次第ですが、在宅ワーカーさんなどに毎日POSTを強制させればちゃんと自宅でやってるかとかそこら辺が判断できるスクリプト。ご利用は計画的に。

まずはHTMLに位置情報を送らせるスクリプトを記述。

index.html

  <script type="text/javascript">
      if (navigator.geolocation) {
          // 現在の位置情報取得を実施
          navigator.geolocation.getCurrentPosition(
          // 位置情報取得成功時
          function (pos) {
                  var location = pos.coords.latitude;
                  location += "," + pos.coords.longitude;
                  document.getElementById("location").innerHTML = location;
          },
          // 位置情報取得失敗時
          function (pos) {
                  var location ="位置情報が取得できませんでした<br />再読み込みでやり直してみてください";
                  document.getElementById("infotext").innerHTML = location;
          });
      } else {
          window.alert("本ブラウザではGeolocationが使えません。");
      }
  </script>

こんな感じです。後はフォームを作ります。UA取るのでそれも忘れず追記。

index.html

  <script type="text/javascript">
  window.onload = function(){
  var UA = navigator.userAgent;
  document.getElementById("result").innerHTML = UA;
  }
  </script>

<form method="POST" action="cgi-bin/index.py">
  <textarea class="hidden" id="location" name="send_geo" readonly></textarea>
  <textarea class="hidden" id="result" name="user" readonly></textarea>
  <select class="form-message validate-required" name="word">
  <option value="in">IN</option>
</select><br>
  <p><input type="submit" value="送信"></p>
</form>

あとはPythonをCGIで動かすと仮定して書きます。そうすればさくらインターネットのレン鯖にもそのまま乗りますしね。

useraccess.cgi

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
from datetime import datetime
import cgi
import csv

print "Content-Type: text/html\n"


print """

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="format-detection" content="telephone=no">
<link media="only screen and (max-device-width:1080px)" href="static/css/smart.css" type="text/css" rel="stylesheet" />
<link media="screen and (min-device-width:1081px)" href="static/css/style.css" type="text/css" rel="stylesheet" />
"""

print """

</head>

<body>
<div id="content">

  <div id="topmain">
"""

try:
    form = cgi.FieldStorage()


    words = form["word"].value
    send_geotag = form["send_geo"].value
    usera = form["user"].value

    today = datetime.now().strftime("%Y/%m/%d %H:%M:%S")

    f = open("date.txt", "ab")
    writer = csv.writer(f, quoting=csv.QUOTE_ALL)
    writer.writerow([words,today,send_geotag,usera])
    f.close()

    print "<p>"+ words + ": " + today + "</p>"

except (RuntimeError, TypeError, KeyError):
    print "<p>"+u"位置情報を許可してやりなおしてください"+"</p>"

print """
</div>
</div>
</body>

</html>
"""


"in","2015/12/03 09:50:33",",","Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13B143 Safari Line/5.7.0"

こんな感じでdata.txtが作られていきます。

細かいエラー分岐なんかはしていないのでそこら辺はなんとかやってください。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?