Tag Archive > ip

Getting Time from an IP Address with Python

» 10 February 2011 » In Internet, Programming, Python » 4 Comments

There are many ways to obtain the current timezone from an IP Address. One is by using a database, and looking up the address. This is an effective way, but the database must be updated frequently. Another way is by using an API that displays location information (and timezone) from a given IP. And this is what I will show you in this post.

Actually, I built a web application that needs the visitor’s current date. And after a couple of hours of googling, I found a reliable API that can help me, IPInfoDB.

The API is pretty simple, and it’s free (registration required). To use it, just pass your API key and the IP address you want to lookup.

http://api.ipinfodb.com/v2/ip_query.php?key=<your_api_key>&ip=75.125.71.106&output=json&timezone=true

It will return something like:

{
  "Status" : "OK",
  "CountryCode" : "US",
  "CountryName" : "United States",
  "RegionCode" : "48",
  "RegionName" : "Texas",
  "City" : "Houston",
  "ZipPostalCode" : "77002",
  "Latitude" : "29.7523",
  "Longitude" : "-95.367",
  "Gmtoffset" : "-21600",
  "Dstoffset" : "0",
  "TimezoneName" : "America/Chicago",
  "Isdst" : "0",
  "Ip" : "75.125.71.106"
}

Now, it gave us enough data to accomplish our task. Well actually, the only data we need is Gmtoffset. The next thing we do is add that to the current UTC time. We can do that in Python:

>>> import datetime
>>> gmtoffset = '-21600'
>>> utctime = datetime.datetime.utcnow()
>>> utctime.strftime('%X %x')
'06:37:48 02/10/11'
>>> iptime = utctime + datetime.timedelta(0, int(gmtoffset))
>>> iptime.strftime('%X %x')
'00:37:48 02/10/11'

Take note that the GMT offset is given in seconds. And if we will convert -21600 seconds to hours, we will have exactly, -6 hours. The datetime.timedelta() method made the addition possible for us, and we just have to supply the number of seconds to add.

Continue reading...

Tags: , , , , ,

Validating IP Address in C#

» 13 December 2010 » In C#, Programming » 4 Comments

It’s been a while since I posted something, mainly because of real life issues. Now, back to the post.

In my coding experience, way back 2007, I use regular expressions to validate IP addresses in every language. Of course it works, but the downside is.. regular expressions. I’m not saying that’s bad, I just feel like it’s being replaced by more precise methods when it comes to validation. And as for C#, it’s not really that hard.

First, you must import System.Net for the IPAddress class. Next is this function.

private bool isvalid_ip(string ip)
{
	IPAddress ot;
	bool valid = false;
	if (string.IsNullOrEmpty(ip))
	{
		valid = false;
	}
	else
	{
		valid = IPAddress.TryParse(ip, out ot);
	}
	return valid;
}

Off: Personally I don’t like how the VS IDE formats the code. The braces looks messy (for me), but for now let’s stick to it.

I don’t even think I have to explain the code, since it’s very much self-explanatory. And I’m currently using this on a subnet mask calculator in C#, which I will be posting when I’m done.

Code in action

Code in action

That’s all for now, and hope you find it useful.

P.S. I do not take credits for the code, this is a neat trick that everybody can think of.

Continue reading...

Tags: , , , , ,

Socks Proxy on Perl

» 22 July 2010 » In Perl » No Comments

Just in case your script needs to transfer/deliver data across the web securely, you might want to add SOCKS support from it.

Continue reading...

Tags: , , , , ,