Tag Archive > address

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: , , , , ,

Protecting Public E-mail Address from Spam

» 23 January 2011 » In Internet, Security » 3 Comments

One of the greatest problems of individuals or group of people is spam. Most of the time, they have their e-mail addresses posted on their public websites for contact purposes. And the addresses is in plain text. This, is one of the most common mistakes, which leads to spam. Encoding contact information in plain text.

I found a solution for this. Well actually two solutions. For a brief summary of how it works, for the first one, the e-mail address is encoded in html entities. The second one includes CSS code direction, the e-mail is somewhat reversed.

All credits to where it is due.

Wanna see it in action? Here is my own e-mail address, this is valid, and you can use this to contact me.

em.leur@leur

The Process

Actually, it’s not so hard. For the first one, encode your e-mail address to html entities. If you have an e-mail called: me@somedomain.com, the equivalent is:

&#109;&#101;&#64;&#115;&#111;&#109;&#101;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;

You can convert them here. And then you can just paste it on your html code.

For the second one, you need to reverse it. For reversing, what I did, is coded a C++ program. Well, you can find tools online, but I found it harder. Yes. So here is the source code if you’re interested.

#include <iostream>
#include <algorithm>
#include <string>
int main() {
	std::string inStr;

	//Take input
	std::cin >> inStr;

	//Reverse it
	std::reverse(inStr.begin(), inStr.end());

	std::cout << inStr << std::endl;
	return 0;
}

Actually, you can do the same in python, as it’s much easier. But I’ve been practicing, so I did this on C++. The output will be:

moc.niamodemos@em

Then, CSS code direction:

<style type="text/css">
	span.codedirection { unicode-bidi:bidi-override; direction: rtl; }
</style>

Basically it will reverse our reversed string. And then wrap it in html span tags (as defined in the CSS above).

<span class="codedirection">moc.niamodemos@em</span>

And, goodbye spam!

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: , , , , ,