Using 3rd python library: pyping

Hi, I’d like to use the following library
https://pypi.org/project/pyping/
in my Python scripts. I downloaded the library and got the following files:

where the pyping folder containes the core.py module with the core functions.
I tried copying the whole folders structure into the /user-lib/pylib ignition folder, but I cannot call any of the library functions and moreover import pyping returns an error.
So: what should I import in order to have that library work in my scripts?

(Ignition version: 7.9.8)

Thanks in advance

What version of Python does it use? 2.5? 2.7? 3?

I don’t know really. The only thing I can grasp from the library homepage is the date of the last commit: 2016. But as a general question: is the way I went right to include a 3rd party library?

I think you’re close: https://support.inductiveautomation.com/index.php?/Knowledgebase/Article/View/98/2/importing-and-using-3rd-party-python-libraries-in-ignition

This library looks to be in Python 3 as well as require root/administrator access to run, so it’s unlikely you’re going to be able to use it.

edit: worth a try if you can get the import working. Looks like you just need the pyping folder and not all the other stuff in the zip. The only reason I have to think it’s Python 3 is because one of the history entries mentions converting to it.

The other question is “why do you need to ping anything?”

Might be able to use something like

import os
os.system("ping " + hostname)

I haven’t done much with that personally but it may be worth some research if you need to ping something.

os.system does not give you any output from the command. I would do either of the following:

  1. Use Popen
from subprocess import Popen,PIPE
p = Popen("ping -n 1 www.google.com",stdout = PIPE)
output,err = p.communicate("")
print output[output.find("time")+5:output.find("TTL")-3]

Or
2. Use a module, depending on your goals

1 Like

Thanks to all for your kind suggestions: I will try all of them.

Just augmenting @Kyle_Chase’s good suggestion. You could create a script in your project script library called inetutils:

import sys
import re
from subprocess import Popen, PIPE

def ping(host, count=1, timeout=1000):
	if sys.platform == 'win32':
		count_arg = '-n'
		timeout_arg = '-w'
	else:
		count_arg = '-c'
		timeout_arg = '-W'
	
	args = map(str, [ 'ping', count_arg, count, timeout_arg, timeout, host ])
	
	p = Popen(args, stdout=PIPE, stderr=PIPE)
	output, err = p.communicate()
	
	if p.returncode == 0:  # success
		try:
			m = re.search("time=(\d+(?:\.\d+){0,1})\s*ms", output)
			time = float(m.group(1))
		except Exception, e:
			time = -1
	else:
		time = None
		
	return (time, p.returncode)

Then, in your project, you can use the something like the following:

from project.inetutils import ping

time, result = ping('google.com')
if result == 0:
	print 'Success, response time is: %.2fms' % time
else:
	print 'Failure, return code: %d' % result

The above solution will leverage the return code of the subprocess call, where 0 is a successful result. Other return codes will produce based on various failures of the call to ping such as 68 for unknown host, or 2 for response timeout. Keep in mind that this is still going to be a blocking call, so use accordingly.

Hope this helps!

2 Likes