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

**URL:** <https://forum.inductiveautomation.com/t/an-error-occurred-nameerror-global-name-socket-is-not-defined/89328>\
**Category:** Ignition\
**Created:** [April 24, 2024, 1:17am UTC](https://forum.inductiveautomation.com/t/an-error-occurred-nameerror-global-name-socket-is-not-defined/89328 "2024-04-24T01:17:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cswett](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@cswett](https://forum.inductiveautomation.com/u/cswett)\
**Post date:** [April 24, 2024, 1:17am UTC](https://forum.inductiveautomation.com/t/an-error-occurred-nameerror-global-name-socket-is-not-defined/89328/1 "2024-04-24T01:17:09Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 24, 2024, 1:27am UTC](https://forum.inductiveautomation.com/t/an-error-occurred-nameerror-global-name-socket-is-not-defined/89328/2 "2024-04-24T01:27:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cswett](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@cswett](https://forum.inductiveautomation.com/u/cswett)\
**Post date:** [April 24, 2024, 1:36am UTC](https://forum.inductiveautomation.com/t/an-error-occurred-nameerror-global-name-socket-is-not-defined/89328/3 "2024-04-24T01:36:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 24, 2024, 7:53am UTC](https://forum.inductiveautomation.com/t/an-error-occurred-nameerror-global-name-socket-is-not-defined/89328/4 "2024-04-24T07:53:07Z")

</div>

Tip: See [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

---

<div class="post-metadata">

**Author:** ![cswett](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@cswett](https://forum.inductiveautomation.com/u/cswett)\
**Post date:** [April 24, 2024, 2:57pm UTC](https://forum.inductiveautomation.com/t/an-error-occurred-nameerror-global-name-socket-is-not-defined/89328/5 "2024-04-24T14:57:14Z")

</div>

That worked perfectly! Thanks Paul!  
Thanks,  
Casey
