('An error occurred:', NameError("global name 'socket' is not defined",))

I'm struggling with getting a socket connection established in a client timer script. I keep getting the same error. Any idea what I'm doing wrong here?

I also tried: from java.net import Socket

('An error occurred:', NameError("global name 'socket' is not defined",))

Ignition 8.1.37

import socket
import sys

Function definition

def send_and_receive(ip, port, message, interval):
print('Config send_and_receive')
sock = None
try:
# try to create a socket using just the IP and port
sock = socket(ip, port)
print('Socket connected')

Thanks,
Casey

Client and gateway event scripts, including timer scripts have weird (bad) scoping.

Write your code in the project library and make your timer script just call into that library and scoping will work as expected.

Also, use Java socket APIs, not the Python/Python ones, for the sake of your future self. Prone to memory and resource leaks and other bugs.

Much appreciated Paul! I will proceed that way!
Again, thank you!
-Casey

Tip: See Wiki - how to post code on this forum.

That worked perfectly! Thanks Paul!
Thanks,
Casey