Created
November 18, 2011 01:12
-
-
Save damoxc/1375209 to your computer and use it in GitHub Desktop.
Ip utilities module for Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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