Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7901775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:19:20+00:00 2026-06-03T09:19:20+00:00

Given an IP Address in dotted quad notation, for example: 192.192.45.1 And a mask

  • 0

Given an IP Address in dotted quad notation, for example:
192.192.45.1
And a mask length for example 8, 16, 24 typically, but could be anything i.e. 17.

Can somebody please provide the code in python to calculate the subnet mask? Preferably I
could get the result as 32-bit integer so that it is easy to hash and then reinterpret as dotted quad when necessary for printing. I see that python has a socket library which is basically a wrapper around the unix socket api. I also saw it has the function inet_ntoa(),
but it returns some sort of packet struct. I’m not terribly familiar with the Python struct library, so I was hoping some other would have some ideas. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:19:22+00:00Added an answer on June 3, 2026 at 9:19 am

    The simplest way is to use google’s ipaddr module. I assume a 25 bit mask below, but as you say, it could be anything

    For Python2

    >>> # Use ipaddr in python 2.x
    >>> import ipaddr
    >>> mask = ipaddr.IPv4Network('192.192.45.1/25')
    >>> mask.netmask
    IPv4Address('255.255.255.128')
    >>>
    

    for Python 3…

    >>> # Use ipaddress in python 3.x
    >>> import ipaddress
    >>> mask = ipaddress.IPv4Network('192.192.45.1/25')
    >>> mask.netmask
    IPv4Address('255.255.255.128')
    >>>
    

    The module is rather efficient at manipulating IPv4 and IPv6 addresses… a sample of some other functionality in it…

    >>> ## Subnet number?
    >>> mask.network
    IPv4Address('192.192.45.0')
    >>>
    >>> ## RFC 1918 space?
    >>> mask.is_private
    False
    >>>
    >>  ## The subnet broadcast address
    >>> mask.broadcast
    IPv4Address('192.192.45.127')
    >>> mask.iterhosts()
    <generator object iterhosts at 0xb72b3f2c>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get an IP address, given a domain name? For example: www.test.com
Given the email address: Jim <jim@example.com> If I try to pass this to MailAddress
Given a literal address, how can I determine which section that address falls in?
How can I look up a hostname given an IP address? Furthermore, how can
How could I write step definitions in Objective-C? E.g.: Given(@^the address book is empty$,
I want to send mail from my android application to given address. But new
I want to send mail from my android application to given address. But now
How can I get the HTML source for a given web address in C#?
I'd like to search for a given MAC address on my network, all from
Given a IPv4 address in the form of a string (ex. 10.171.24.69) and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.