Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » 32-bit integers IP address to 4-octet IP address [RSS Feed]

#1 Jan. 23, 2011 18:19:42

a.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

32-bit integers IP address to 4-octet IP address


I guess socket.inet_ntoa is enough for what you need.

On Sun, Jan 23, 2011 at 7:06 AM, Phillip Bailey <phil...@bailey.st> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
>
> Dear All,
>
> I've started playing around with Django in the last few days, and I'm
> enjoying the speed
> and the power of such framework. I'm writing a small application to
> display and sort
> ip addresses stored in a Postgresql database, right now I'm stuck with
> 32-bit integers
> IP address translated to 4-octet IP address .
>
> Here's the the class of the ip address.
>
> class Iphdr(models.Model):
>    sid = models.IntegerField(primary_key=True)
>    cid = models.BigIntegerField(primary_key=True)
>    ip_src = models.BigIntegerField()
>    ip_dst = models.BigIntegerField()
>    ip_ver = models.SmallIntegerField()
>    ip_hlen = models.SmallIntegerField()
>    ip_tos = models.SmallIntegerField()
>    ip_len = models.IntegerField()
>    ip_id = models.IntegerField()
>    ip_flags = models.SmallIntegerField()
>    ip_off = models.IntegerField()
>    ip_ttl = models.SmallIntegerField()
>    ip_proto = models.SmallIntegerField()
>    ip_csum = models.IntegerField()
>    class Meta:
>        db_table = u'iphdr'
>
>
> As the IP addresses are stored in the database as unsigned 32-bit
> integers, so when I
> run  ip = Iphdr.objects.values("ip_src") the result is in 32-bit integers,
>
> {'ip_src': 3251031814L}{'ip_src': 3251031816L}{'ip_src': 3251031816L}
> {'ip_src': 3251031816L}{'ip_src': 3251031814L}{'ip_src': 3251031816L}
>
> There's any elegant solution that can translate 32-bit integers to
> 4-octet IP address (192.168.1.88) ?
>
> Thanks in advance.
>
> Phillip
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
>
> iQEcBAEBAgAGBQJNO++eAAoJENNBJKHbaDgTM0cH/0/QjdPTi53tJG4YhjVpt6oj
> cJk6rAEHwFha//Vby4lH2sT+ZyKmv+UAjSVmvBCptXBOcm5LuedMmMCVeInfuliQ
> GeOPlQ4nvrZSbcdXirLu9Jqa+Yqj6nalGF7i0WetqvdsdrkQQdrcx1D814DSZKGq
> qQWpdyVEmdrRSfiOUvrYbnXDCnTwCXZdUhcpDNmD+5h9S2/uiMzP5cM2wtfMg1hO
> swYS9B7h/6vAd1ekOZh+ij2X4mszUfp5rQzFjCXtYnQma8pN+58UMpWwYVKj2FzA
> tgRaWC1Qz4hWsXRvwirqo/ZtyJMWxkOdwVaxl3iug/NCGW/yDCMa97zweYOHKCs=
> =W+IC
> -----END PGP SIGNATURE-----
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>
--
's
Aledr - Alexandre
"OpenSource Solutions for SmallBusiness Problems"

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 23, 2011 22:36:50

p.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

32-bit integers IP address to 4-octet IP address


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I aledr,

thanks for your reply,


I've added the socket.inet_ntoa to my view.py

#view.py
from django.http import HttpResponse
import socket
from snort.models import Iphdr


def snort(request):

ip = Iphdr.objects.values("ip_src")
return socket.inet_ntoa(ip)


after opening the web page I get this error bac





Request Method: GET
Request URL:http://192.168.1.5:8080/Django Version: 1.3 beta 1
Exception Type: TypeError
Exception Value:

inet_aton() argument 1 must be string, not ValuesQuerySet

Exception Location: /home/user/django/app/../snort/views.py in snort,
line 10
Python Executable: /usr/bin/python
Python Version: 2.6.5
Python Path:


~


What I'm doing wrong ?

Thanks again,

Phillip



On 01/23/2011 07:18 PM, aledr wrote:
> I guess socket.inet_ntoa is enough for what you need.
>
> On Sun, Jan 23, 2011 at 7:06 AM, Phillip Bailey <phil...@bailey.st> wrote:
>
>
> Dear All,
>
> I've started playing around with Django in the last few days, and I'm
> enjoying the speed
> and the power of such framework. I'm writing a small application to
> display and sort
> ip addresses stored in a Postgresql database, right now I'm stuck with
> 32-bit integers
> IP address translated to 4-octet IP address .
>
> Here's the the class of the ip address.
>
> class Iphdr(models.Model):
> sid = models.IntegerField(primary_key=True)
> cid = models.BigIntegerField(primary_key=True)
> ip_src = models.BigIntegerField()
> ip_dst = models.BigIntegerField()
> ip_ver = models.SmallIntegerField()
> ip_hlen = models.SmallIntegerField()
> ip_tos = models.SmallIntegerField()
> ip_len = models.IntegerField()
> ip_id = models.IntegerField()
> ip_flags = models.SmallIntegerField()
> ip_off = models.IntegerField()
> ip_ttl = models.SmallIntegerField()
> ip_proto = models.SmallIntegerField()
> ip_csum = models.IntegerField()
> class Meta:
> db_table = u'iphdr'
>
>
> As the IP addresses are stored in the database as unsigned 32-bit
> integers, so when I
> run ip = Iphdr.objects.values("ip_src") the result is in 32-bit integers,
>
> {'ip_src': 3251031814L}{'ip_src': 3251031816L}{'ip_src': 3251031816L}
> {'ip_src': 3251031816L}{'ip_src': 3251031814L}{'ip_src': 3251031816L}
>
> There's any elegant solution that can translate 32-bit integers to
> 4-octet IP address (192.168.1.88) ?
>
> Thanks in advance.
>
> Phillip
>>
- --
You received this message because you are subscribed to the Google
Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.
>>
>>
> --
> 's
> Aledr - Alexandre
> "OpenSource Solutions for SmallBusiness Problems"

- --
Snorby SSD: The IDS (Intrusion Detection System) Linux distributionhttp://bailey.st/blog/snorby-spsa/-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iQEcBAEBAgAGBQJNPK10AAoJENNBJKHbaDgTKN0IAL7YlR/rmtcrj9GDbTCUElu4
81t+0Y8let5XgnvYGVjbaUyPP+BwxhoWpN8f6PqFfohPbj+Xt9EuSdJIV9Wr84Bi
NLhWtQyw/ioFZaDH1b50smEfR1qqIsQRd+wfbor21cwe5YhjorZ9FuPoH1HBFFPp
4kct+b0vMuGb0OjlWRosVAe/p3mzbNbn8glVhqaBthAl6GmAOrBuEmRqdB6jgN2s
/fLNGKi08YdlbHn5hvnmXTONiFQf5aA40ZpKlTmCWHJUiVINfuUw7FE5E/ZuYve2
l7Vs2BIIKy3X4oRFZbeQXV/yVyOKygIwdmzJZ+iBLL4zdST8YA3Zj/8Q73pX434=
=6lwe
-----END PGP SIGNATURE-----

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 24, 2011 02:30:22

a.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

32-bit integers IP address to 4-octet IP address


>From the Django docs:

"values() takes optional positional arguments, *fields, which specify
field names to which the SELECT should be limited. If you specify the
fields, each dictionary will contain only the field keys/values for
the fields you specify. If you don't specify the fields, each
dictionary will contain a key and value for every field in the
database table."

So, when you try "ip = Iphdr.objects.values("ip_src")" you are not
getting the 32 bit integer you want.

:http://docs.djangoproject.com/en/dev/ref/models/querysets/On Sun, Jan 23, 2011 at 8:36 PM, phil...@bailey.st <phil...@bailey.st> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I aledr,
>
> thanks for your reply,
>
>
> I've added the  socket.inet_ntoa to my  view.py
>
> #view.py
> from django.http import HttpResponse
> import socket
> from snort.models import  Iphdr
>
>
> def snort(request):
>
>    ip = Iphdr.objects.values("ip_src")
>    return socket.inet_ntoa(ip)
>
>
> after opening the web page I get this error bac
>
>
>
>
>
> Request Method:         GET
> Request URL:http://192.168.1.5:8080/> Django Version:         1.3 beta 1
> Exception Type:         TypeError
> Exception Value:
>
> inet_aton() argument 1 must be string, not ValuesQuerySet
>
> Exception Location:     /home/user/django/app/../snort/views.py in snort,
> line 10
> Python Executable:      /usr/bin/python
> Python Version:         2.6.5
> Python Path:
>
>
> ~
>
>
> What I'm doing wrong ?
>
> Thanks again,
>
> Phillip
>
>
>
> On 01/23/2011 07:18 PM, aledr wrote:
>> I guess socket.inet_ntoa is enough for what you need.
>>
>> On Sun, Jan 23, 2011 at 7:06 AM, Phillip Bailey <phil...@bailey.st> wrote:
>>
>>
>> Dear All,
>>
>> I've started playing around with Django in the last few days, and I'm
>> enjoying the speed
>> and the power of such framework. I'm writing a small application to
>> display and sort
>> ip addresses stored in a Postgresql database, right now I'm stuck with
>> 32-bit integers
>> IP address translated to 4-octet IP address .
>>
>> Here's the the class of the ip address.
>>
>> class Iphdr(models.Model):
>>    sid = models.IntegerField(primary_key=True)
>>    cid = models.BigIntegerField(primary_key=True)
>>    ip_src = models.BigIntegerField()
>>    ip_dst = models.BigIntegerField()
>>    ip_ver = models.SmallIntegerField()
>>    ip_hlen = models.SmallIntegerField()
>>    ip_tos = models.SmallIntegerField()
>>    ip_len = models.IntegerField()
>>    ip_id = models.IntegerField()
>>    ip_flags = models.SmallIntegerField()
>>    ip_off = models.IntegerField()
>>    ip_ttl = models.SmallIntegerField()
>>    ip_proto = models.SmallIntegerField()
>>    ip_csum = models.IntegerField()
>>    class Meta:
>>        db_table = u'iphdr'
>>
>>
>> As the IP addresses are stored in the database as unsigned 32-bit
>> integers, so when I
>> run  ip = Iphdr.objects.values("ip_src") the result is in 32-bit integers,
>>
>> {'ip_src': 3251031814L}{'ip_src': 3251031816L}{'ip_src': 3251031816L}
>> {'ip_src': 3251031816L}{'ip_src': 3251031814L}{'ip_src': 3251031816L}
>>
>> There's any elegant solution that can translate 32-bit integers to
>> 4-octet IP address (192.168.1.88) ?
>>
>> Thanks in advance.
>>
>> Phillip
>>>
> - --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>>>
>>>
>> --
>> 's
>> Aledr - Alexandre
>> "OpenSource Solutions for SmallBusiness Problems"
>
> - --
> Snorby SSD: The IDS (Intrusion Detection System) Linux distribution
>http://bailey.st/blog/snorby-spsa/> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
>
> iQEcBAEBAgAGBQJNPK10AAoJENNBJKHbaDgTKN0IAL7YlR/rmtcrj9GDbTCUElu4
> 81t+0Y8let5XgnvYGVjbaUyPP+BwxhoWpN8f6PqFfohPbj+Xt9EuSdJIV9Wr84Bi
> NLhWtQyw/ioFZaDH1b50smEfR1qqIsQRd+wfbor21cwe5YhjorZ9FuPoH1HBFFPp
> 4kct+b0vMuGb0OjlWRosVAe/p3mzbNbn8glVhqaBthAl6GmAOrBuEmRqdB6jgN2s
> /fLNGKi08YdlbHn5hvnmXTONiFQf5aA40ZpKlTmCWHJUiVINfuUw7FE5E/ZuYve2
> l7Vs2BIIKy3X4oRFZbeQXV/yVyOKygIwdmzJZ+iBLL4zdST8YA3Zj/8Q73pX434=
> =6lwe
> -----END PGP SIGNATURE-----
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>
--
's
Aledr - Alexandre
"OpenSource Solutions for SmallBusiness Problems"

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » 32-bit integers IP address to 4-octet IP address [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support