ASIC/FPGA Design and Verification Out Source Services
My first python script.
-
Recently I started to study python .
In this site I present many
scripts.
For simple tasks, I use bash and for
more complex ones perl. Since my daughter starts
to study
python , so do
I.
-
This script extracts the current time from a web site, translates it to IL
local time and prints the results.
-
First I needed to check if the library request is installed on my
debian machine. So I fired the interactive
python and gave the import command:
>>> import requests
Traceback (most recent call last):
File "", line 1, in
ImportError:
No module named requests
To install the library, I used:
apt-cache search python-requests
python-requests - elegant and simple HTTP library for Python, built for human beings
apt-get install python-requests
-
The script is simple and is built of the following:
- library import,
- read web page into memory,
- extract time using regular expression,
- print results
The code is listed below:
#!/usr/bin/python
#process web pages
import requests
#handle regular expression
import re
#Get the time from the web
#Look for the following string:... 07:14:02 UTC ...
response = requests.get('http://tycho.usno.navy.mil/cgi-bin/timer.pl')
m = re.search('[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]* UTC', response.content[:500])
print "found", m.group(0)
str = m.group(0).replace(" UTC", "")
Lname=[]
Lname.append("hour")
Lname.append("min ")
Lname.append("sec ")
array=str.split(':')
ix=0
for ti in array:
if(ix == 0):
#adjust to IL time
ti_i = int(ti) + 3
print Lname[ix], ti_i
else:
print Lname[ix], ti
ix += 1
|