Package: pythonping
pythonpingPythonPing is simple way to ping in Python. With it, you can send ICMP Probes to remote devices like
you would do from the terminal. PythonPing is modular, so that you can run it in a script as a
standalone function, or integrate its components in a fully-fledged application.Basic UsageThe simplest usage of PythonPing is in a script. You can use thepingfunction to ping a target.
If you want to see the output immediately, emulating what happens on the terminal, use theverboseflag as below.frompythonpingimportpingping('127.0.0.1',verbose=True)This will yeld the following result.Reply from 127.0.0.1, 9 bytes in 0.17ms
Reply from 127.0.0.1, 9 bytes in 0.14ms
Reply from 127.0.0.1, 9 bytes in 0.12ms
Reply from 127.0.0.1, 9 bytes in 0.12msRegardless of the verbose mode, thepingfunction will always return aResponseListobject.
This is a special iterable object, containing a list ofResponseitems. In each response, you can
find the packet received and some meta information, like the time it took to receive the response
and any error message.You can also tune your ping by using some of its additional parameters:sizeis an integer that allows you to specify the size of the ICMP payload you desiretimeoutis the number of seconds you wish to wait for a response, before assuming the target
is unreachablepayloadallows you to use a specific payload (bytes)countspecify allows you to define how many ICMP packets to sendintervalthe time to wait between pings, in secondssweep_startandsweep_endallows you to perform a ping sweep, starting from payload size
defined insweep_startand growing up to size defined insweep_end. Here, we repeat the payload
you provided to match the desired size, or we generate a random one if no payload was provided.
Note that if you definedsize, these two fields will be ignoreddfis a flag that, if set to True, will enable theDon't Fragmentflag in the IP headerverboseenables the verbose mode, printing output to a stream (seeout)outis the target stream of verbose mode. If you enable the verbose mode and do not provideout, verbose output will be send to thesys.stdoutstream. You may want to use a file here.matchis a flag that, if set to True, will enable payload matching between a ping request
and reply (default behaviour follows that of Windows which counts a successful reply by a
matched packet identifier only; Linux behaviour counts a non equivalent payload with a matched
packet identifier in reply as fail, such as when pinging 8.8.8.8 with 1000 bytes and the reply
is truncated to only the first 74 of request payload with a matching packet identifier)FAQDo I need privileged mode or root?Yes, you need to be root to use pythonping.Why do I need to be root to use pythonping?All operating systems allow programs to create TCP or UDP sockets without requiring particular
permissions. However, ping runs in ICMP (which is neither TCP or UDP). This means we have to create
raw IP packets, and sniff the traffic on the network card.Operating systems are designed to require root for such operations. This is because having
unrestricted access to the NIC can expose the user to risks if the application running has bad
intentions. This is not the case with pythonping of course, but nonetheless we need this capability
to create custom IP packets. Unfortunately, there is simply no other way to create ICMP packets.Advanced UsageIf you wish to extend PythonPing, or integrate it in your application, we recommend to use the
classes that are part of Python Ping instead of thepingfunction.executor.Communicatorhandles the communication with the target device, it takes care of sending ICMP requests and
processing responses (note that for it to be thread safe you must then handle making a unique
seed ID for each thread instance, see ping._init_for an example of this). It ultimately
produces theexecutor.ResponseListobject. TheCommunicatorneeds to know a target and
which payloads to send to the remote device. For that, we have several classes in thepayload_providermodule. You may want to create your own provider by extendingpayload_provider.PayloadProvider. If you are interested in that, you should check the
documentation of bothexecutorandpayload_providermodule.Code StructureTop Level Directory LayoutOur project directory structure contains all src files in the pythonping folder, test cases in another folder, and helping documentation in on the top level directory..
├── pythonping              # Source files 
├── test                    # Automated Testcases for the package
├── CODE_OF_CONDUCT         # An md file containing code of conduct
├── CONTRIBUTING            # Contributing Guidlins
├── LICENSE                 # MIT License
├── README.md               # An md file
└── setup.py                # InstalationA UML Diagram of the code structure is below:As per the uml diagram above five distinct classes outside of init exist in this package: Executor, Icmp, Payload Provider, and Utils. Each of them rely on attributes which have been listed as sub-classes for brevities sake. An overview of each class is as follows.UtilsSimply generates random text. See function random_text.NetworkOpens a socket to send and recive data. See functions send, recv, and del.Payload ProviderGenerates ICMP Payloads with no Headers. It's functionaly a interface. It has three
functions init, iter, and next, which are all implmented by subclasses List, Repeat, and Sweep which store payloads in diffrent lists.ICMPGenerates the ICMP heaser through subclass ICMPType, and various helper functions.ExecutorHas various subclasses including Message, Response, Success, and Communicator used for sending icmp packets and collecting data.InitUses network, executor, payload_provider and utils.random_text to construct and send ICMP packets to ping a network.TestsA test package exists under the folder test, and contains a serise of unit tests. Before commiting changes make sure to run the test bench and make sure all corrisponding cases pass. For new functionality new test cases must be added and documented.To run testcases we can simply use theunitest discoverutility by running the following command:python -m unittest discover <test_directory>To run the test cases in a specific file FILE we must run the following command:python -m unittest discover -s <test_directory> -p FILEAnother option is to run the following from the top level directory:pytest testTo test for coverage simply run:coverage run -m pytest testContributingBefore contributing read through the contribution guidlines found the CONTRIBUTING file.Code StyleA few key points when contributing to this repo are as follows:Use tabs over spaces.Format doc strings as such:DESCRIPTION

    :param X: DESCRIPTION
    :type X: Type
    :param Y: DESCRIPTION
    :type Y: TypePlease add doc strings to all functions added.Do not add spaces between docstring and first function line.Do not go over 200 characters per line.When closing multiline items under brackets('()', '[]', ... etc) put the closing bracket on it's own line.