Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Control if a input text is IP

Reply
Thread Tools

Re: Control if a input text is IP

 
 
Fredrik Lundh
Guest
Posts: n/a
 
      02-02-2006
Sbaush wrote:

> My app has in input an ip address in IPv4 notation.
> is there a function that control if input is a string in IPv4 notation?


here's one way to do it:

def ipcheck(s):
try:
a, b, c, d = [chr(int(c)) for c in s.split(".")]
except ValueError:
return False
else:
return True

another way is to use regular expressions; see

http://www.regular-expressions.info/examples.html

</F>



 
Reply With Quote
 
 
 
 
Fuzzyman
Guest
Posts: n/a
 
      02-03-2006

Fredrik Lundh wrote:
> Sbaush wrote:
>
> > My app has in input an ip address in IPv4 notation.
> > is there a function that control if input is a string in IPv4 notation?

>
> here's one way to do it:


Here's a function from the 'validate' module that comes with ConfigObj
:

def dottedQuadToNum(ip):
"""
Convert decimal dotted quad string to long integer

>>> dottedQuadToNum('1 ')

1L
>>> dottedQuadToNum(' 1.2')

16777218L
>>> dottedQuadToNum(' 1.2.3 ')

16908291L
>>> dottedQuadToNum('1.2.3.4')

16909060L
>>> dottedQuadToNum('1.2.3. 4')

Traceback (most recent call last):
ValueError: Not a good dotted-quad IP: 1.2.3. 4
>>> dottedQuadToNum('255.255.255.255')

4294967295L
>>> dottedQuadToNum('255.255.255.256')

Traceback (most recent call last):
ValueError: Not a good dotted-quad IP: 255.255.255.256
"""

# import here to avoid it when ip_addr values are not used
import socket, struct

try:
return struct.unpack('!L',
socket.inet_aton(ip.strip()))[0]
except socket.error:
# bug in inet_aton, corrected in Python 2.3
if ip.strip() == '255.255.255.255':
return 0xFFFFFFFFL
else:
raise ValueError('Not a good dotted-quad IP: %s' % ip)
return

All the best,


Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Width of text input box vs. password input box cjl HTML 1 10-31-2005 11:46 AM
Off Topic: Width of text input box vs. password input box cjl Javascript 7 10-30-2005 10:11 PM
can i pass data with input text instead of input hidden? Walter ASP General 6 09-17-2004 11:16 AM
Color of a text in a <input type=text> when the tag is DISABLED Romeyer Olivier HTML 12 01-28-2004 01:37 AM
why does form with only 1 text input and 1 button input submit on enter? Guy HTML 5 12-13-2003 06:44 AM



Advertisments