Package: iplib3
IPLib3Apathlib.Path-equivalent for IP addresses.PyPITests (main)Tests (nightly)DocsActivityQAOtherThis module was heavily inspired by the built-inpathlibmodule to provide a similar, flexible interface for IP addresses.iplib3can effortlessly convert between IPv4, IPv6, raw numbers and hex values and it can also verify IP address syntax. It can recognise optional port numbers and store them separately from the main address. Theiplib.IPAddressclass works likepathlib.Pathin that it accepts both IPv4 and IPv6 addresses, returning an object representing whichever format was used. The module also uses some unit tests, and these will be added more over time as functionality grows and becomes more set in stone.The module is currently lacking in long-term vision as I used it as a practice project, but there are some plans to further flesh it out. It could incorporate URL support in the future and may be extended withrequestsintegration.This project is not affiliated withiplib, the naming similarity is merely a coincidence.Installationiplib3will work with Python versions 3.6 and above. It could be back-ported to 3.5 or for even earlier versions with relatively little change, but the maintainer doesn't see a good reason to focus on earlier versions.This module will not support Python 2.Use the package managerpipto installiplib3.On Windows:py-mpipinstalliplib3On most Unix-like platforms:pip3installiplib3On other platforms, you may try:pipinstalliplib3The library is distributed as wheels and source distributions, and the source distributions use Poetry as the build back-end instead ofsetuptools.UsageThe module mainly provides a single class,iplib3.IPAddress, which can be used to initialise IP address objects of any supported type. However, it is possible to use the providediplib3.IPv4andiplib3.IPv6classes directly if needed.The primary class has the advantage that it also supports raw numbers; you can initialise it with any positive integer in addition to stringified addresses, and since you can directly convert between the two sub-classes at any time you can use all functionality with justiplib3.IPAddress. Sinceiplib3.IPv4andiplib3.IPv6are subclasses ofiplib3.IPAddress, you can useisinstanceto recognise any of the three types.Some basic usage examples:fromiplib3importIPAddress,IPv6address=IPAddress('222.173.190.239')print(address)# 222.173.190.239print(address.port)# None, because we never specified oneaddress.port=80print(address)# 222.173.190.239:80print(repr(address))# iplib3.IPv4('222.173.190.239:80')ipv6_address=address.as_ipv6print(ipv6_address)# [0:0:0:0:0:0:DEAD:BEEF]:80ipv6_address.port=Noneprint(ipv6_address)# 0:0:0:0:0:0:DEAD:BEEFprint(repr(ipv6_address))# iplib3.IPv6('0:0:0:0:0:0:DEAD:BEEF')foo=IPv6('[::1337:1337:1337:1337]:25565')bar=IPv6('::1337:1337:1337:1337',25565)baz=IPv6('::1337:1337:1337:1337',port_num=25565)print(f"Addresses are{'equal'iffoo==bar==bazelse'not equal'}")print(baz)# [::1337:1337:1337:1337]:25565print(baz.as_ipv4.as_ipv6==baz)# If the string contains a port number and you also provide a port separately,# then the separately provided port takes precedencespam=IPv6('[::1337:1337:1337:1337]:80',port_num=25565)print(spam)# [::1337:1337:1337:1337]:25565print(address.hex)# 0xDEADBEEFprint(address.num)# 3735928559ContributingPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.Please make sure to update tests as appropriate.LicenseThis project is licensed under anMIT license.