Skip to content

Instantly share code, notes, and snippets.

@damoxc
Created November 18, 2011 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damoxc/1375209 to your computer and use it in GitHub Desktop.
Save damoxc/1375209 to your computer and use it in GitHub Desktop.
Ip utilities module for Python
from libc.stdio cimport sprintf
from libc.string cimport strlen, strtok
def reverse_ip(ip):
cdef int ret
cdef char *_ip = ip, *p1, *p2, *p3, *p4, rip[16]
if strlen(_ip) > 15:
raise ValueError('Invalid IP address')
p1 = strtok(_ip, '.')
p2 = strtok(NULL, '.')
p3 = strtok(NULL, '.')
p4 = strtok(NULL, '.')
# Normally using snprintf here would be wise but since there is a
# previous check for the length of the string we are splitting and
# reversing it's safe to use sprintf in this instance as it is faster.
sprintf(rip, "%s.%s.%s.%s", p4, p3, p2, p1)
return rip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment