| Testing if hostname is numeric IPv4Posted on August 31, 2010 by Mikko OhtamaaFiled Under iphone, mobile, python, technology I had to resort this hack when testing a hybrid web/mobile site which uses site hostname based device discrimination. In production mode we can have m.yoursite.com and www.yoursite.com hostnames. However, when running the site locally, on your development computer and in LAN this does not work very well: one cannot spoof hostnames for web browsers in devices like iPhone/iPod/other mobile phone unless you install a DNS server. And installing a DNS server for LAN is something you don’t want to do… So, I figured out that I can use hostname spoofing on desktop computers (/etc/hosts file) and I always access the site via numeric IP (IPv4 over ethernet) when testing over WLAN on mobile devices.
And,… dadaa,… here is my magical code to test whether hostname is numeric IPv4. I couldn’t find a ready function from Python standard library import re
ipv4_regex_source = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
ipv4_regex = re.compile(ipv4_regex_source)
def is_numeric_ipv4(str):
"""
http://answers.oreilly.com/topic/318-how-to-match-ipv4-addresses-with-regular-expressions/
@param str: Hostname as a string.
@return: True if the given string is numeric IPv4 address
"""
# ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
return ipv4_regex.match(str)
Python-like urlparser module for JavascriptPosted on November 29, 2008 by Mikko OhtamaaFiled Under Uncategorized I had to deconstruct and reconstruct URLs from pieces when doing advanced Javascripting for Plone. I found this nice library from Denis Laprise. However, it had a bug with fragment extractor and lacked reconstruction possibilies. So I decided to make a new version. Download urlparse.js version 0.2. thank you Couple of examples: var p = new Poly9.URLParser('http://user:password@poly9.com/pathname?arguments=1#fragment');
p.getHost() == 'poly9.com';
p.getProtocol() == 'http';
p.getPathname() == '/pathname';
p.getQuerystring() == 'arguments=1';
p.getFragment() == 'fragment';
p.getUsername() == 'user';
p.getPassword() == 'password';
var p = new Poly9.URLParser("http://localhost:8080/path);
p.setQuerystring("foo=bar");
p.setFragment("anchor");
p.setPort(7070);
var url = p.getURL() // http://localhost:7070/path?foo=bar#anchor
|
