[JAVA] Get your current location and user agent in Python

Well, it depends on the person, but if you force a home worker etc. to POST every day, you can judge whether you are doing it at home or not. Use it systematically.

First, write a script that sends location information to HTML.

index.html



  <script type="text/javascript">
      if (navigator.geolocation) {
          //Acquire current location information
          navigator.geolocation.getCurrentPosition(
          //When location information acquisition is successful
          function (pos) {
                  var location = pos.coords.latitude;
                  location += "," + pos.coords.longitude;
                  document.getElementById("location").innerHTML = location;
          },
          //When location information acquisition fails
          function (pos) {
                  var location ="Location information could not be obtained<br />Try reloading and try again";
                  document.getElementById("infotext").innerHTML = location;
          });
      } else {
          window.alert("Geolocation cannot be used with this browser.");
      }
  </script>

It is like this. The rest is to make a form. I will take UA, so don't forget to add it.

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="Send"></p>
</form>

The rest is written assuming that Python runs on CGI. That way, you can get on Sakura Internet's Ren Saba as it is.

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"Please allow location information and try again"+"</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 will be created like this.

I haven't done any small error branching, so please do something about it.

Recommended Posts

Get your current location and user agent in Python
Parse User Agent in Python
Get the current date and time in Python, considering the time difference
Get your own IP address in Python
Get a list of packages installed in your current environment with python
Get date in Python
python get current time
Get your current location with a USB GPS receiver
Get stock prices and create candlestick charts in Python
[Python] Get user information and article information with Qiita API
Get last month in python
Get Terminal size in Python
Explicitly get EOF in python
Stack and Queue in Python
Unittest and CI in Python
[python] Get quotient and remainder
Get Evernote notes in Python
Get Japanese synonyms in Python
Get your heart rate from the fitbit API in Python!
Get the MIME type in Python and determine the file format
Get Leap Motion data in Python.
MIDI packages in Python midi and pretty_midi
Difference between == and is in python
[Python] logging in your own module
View photos in Python and html
Sorting algorithm and implementation in Python
Get data from Quandl in Python
Get the desktop path in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Get the script path in Python
Create your own graph structure class and its drawing in python
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Get, post communication memo in Python
Function synthesis and application in Python
Export and output files in Python
Get the desktop path in Python
Reverse Hiragana and Katakana in Python2.7
Get the host name in Python
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Python Note: Get the current month
Get started with Python in Blender
Get the title and delivery date of Yahoo! News in Python
Create and read messagepacks in Python
Specify your own class in class argument and return type annotation in Python
How to get the date and time difference in seconds with python
Get parameter and Post body, enable CORS in Flask (Python) and Express (Node.js)
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Get options in Python from both JSON files and command line arguments
Overlapping regular expressions in Python and Java
Get additional data in LDAP with python
Differences in authenticity between Python and JavaScript
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Create your own Linux commands in Python
Get exchange rates from open exchange rates in Python
AM modulation and demodulation in Python Part 2
Get Suica balance in Python (using libpafe)