# Regarding Gateway Event Script - Tag Chabge

**URL:** <https://forum.inductiveautomation.com/t/regarding-gateway-event-script-tag-chabge/102950>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [February 26, 2025, 11:00pm UTC](https://forum.inductiveautomation.com/t/regarding-gateway-event-script-tag-chabge/102950 "2025-02-26T23:00:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Parmar](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Rakesh\_Parmar](https://forum.inductiveautomation.com/u/Rakesh_Parmar)\
**Post date:** [February 26, 2025, 11:00pm UTC](https://forum.inductiveautomation.com/t/regarding-gateway-event-script-tag-chabge/102950/1 "2025-02-26T23:00:19Z")

</div>

Hi,

I have write following script in "Script Console" & It is working fine and giving me result

```python
import socket
import time
import re

def send_command(command, delay_time=5, timeout=30):
    host = "192.168.0.101" # Printer's IP address
    port = 3000 # TCP port for AlphaJet printer

    s = None
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, port))
        s.settimeout(timeout)

        # Wait for "Connection Accepted" response
        response = s.recv(4096).decode()

        if "Connection Accepted" in response:
            s.sendall(command.encode())
            response = s.recv(4096).decode()
        else:
            return None

        time.sleep(delay_time)
        return response

    except socket.timeout:
        return None

    except Exception as e:
        return None

    finally:
        if s:
            s.close()

def extract_label_name(response):
    match = re.search(r'<LOADLAB>(.*?)</LOADLAB>', response)
    return match.group(1) if match else "N/A"

def extract_data(response):
    cable_match = re.search(r'aPvn="CABLE NUMBER">(\d+)', response)
    jacket_match = re.search(r'aPvn="jacket line ">(\d+)', response)

    cable = cable_match.group(1) if cable_match else "N/A"
    jacket = jacket_match.group(1) if jacket_match else "N/A"

    return cable, jacket

def write_to_ignition_tags(label_name, cable, jacket):
    try:
        system.tag.writeBlocking([
            "[Sample_Tags]Label_Name",
            "[Sample_Tags]Cable_Number",
            "[Sample_Tags]Jacket_Line"
        ], [label_name, cable, jacket])
    except Exception as e:
        print("Failed to write tags: {}".format(e))

# Step 1: Get the label name
cmd_loadlab = """<GP><LOADLAB/></GP>"""
response_loadlab = send_command(cmd_loadlab)

if response_loadlab:
    label_name = extract_label_name(response_loadlab)

    # Step 2: Use the label name to get details
    cmd_version = """<GP><LOADLAB aPrompt="1">\\{}</LOADLAB></GP>""".format(label_name)
    response_version = send_command(cmd_version, delay_time=10)

    # Step 3: Extract and write data to Ignition tags
    if response_version:
        cable, jacket = extract_data(response_version)
        print("\nExtracted Information:")
        print("Label Name: {}".format(label_name))
        print("CABLE NUMBER: {}".format(cable))
        print("jacket line: {}".format(jacket))
        write_to_ignition_tags(label_name, cable, jacket)

```

Response is,

```python
Extracted Information:
Label Name: label\12 FIBER MLT DUCT-LITE 2024.txt
CABLE NUMBER: 0457
jacket line: 1
>>>

```

Also I getting the same information in memory tag as well.  
(Pls see attached file Capture1.PNG)

but when I use the same script on the "gateway event script", It is not working means memory Tag is not updating.  
(Pls see attached file Capture2.PNG , Capture3.PNG & Capture4.PNG)

Can anyone tell me what is wrong here ?  
How Can I correct it ?  
Pls note that I am new to the Ignition.

 ![Capture1](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/0/609011133863b483d272fa9cd02899ccb2c73a83.png)  
 ![Capture2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/8/6803cf7db377ec1e28803293e1189eeb2f7a37ae.png)  
 ![Capture3](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/3/5314818b1832ab29966ba2fe1f8e9d46f69f1816.png)

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parmar](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Rakesh\_Parmar](https://forum.inductiveautomation.com/u/Rakesh_Parmar)\
**Post date:** [February 26, 2025, 11:02pm UTC](https://forum.inductiveautomation.com/t/regarding-gateway-event-script-tag-chabge/102950/2 "2025-02-26T23:02:37Z")

</div>

I am attaching here "Capture4.PNG" file.

 ![Capture4](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/3/93218b359b7978667ced5bf2a1229ec76edd6bc4.png)

---

<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:** [February 26, 2025, 11:06pm UTC](https://forum.inductiveautomation.com/t/regarding-gateway-event-script-tag-chabge/102950/3 "2025-02-26T23:06:00Z")

</div>

A gateway tag change script is running _on_ the gateway. Does the Ignition Gateway service on whatever your host OS is:

1. Have access to the relevant IP address
2. Have access to the relevant port
3. Have the ability to open raw sockets in the OS

Are there any errors in the gateway's logs?

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parmar](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Rakesh\_Parmar](https://forum.inductiveautomation.com/u/Rakesh_Parmar)\
**Post date:** [February 26, 2025, 11:23pm UTC](https://forum.inductiveautomation.com/t/regarding-gateway-event-script-tag-chabge/102950/4 "2025-02-26T23:23:51Z")

</div>

I have installed the Ignition gateway on my Laptop so I am using the "localhost:8088" to access the gateway.

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parmar](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Rakesh\_Parmar](https://forum.inductiveautomation.com/u/Rakesh_Parmar)\
**Post date:** [February 26, 2025, 11:43pm UTC](https://forum.inductiveautomation.com/t/regarding-gateway-event-script-tag-chabge/102950/5 "2025-02-26T23:43:22Z")

</div>

Please find the error details,

 ![Capture5](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/e/4e718e0bea4dc95a60703cb3311fb03c01b58b28.jpeg)

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 62, in File "", line 11, in send\_command NameError: global name 'socket' is not defined

at org.python.core.Py.NameError(Py.java:259)

at org.python.core.PyFrame.getglobal(PyFrame.java:265)

at org.python.pycode.\_pyx0.send\_command$1(:35)

at org.python.pycode.\_pyx0.call\_function()

at org.python.core.PyTableCode.call(PyTableCode.java:173)

at org.python.core.PyBaseCode.call(PyBaseCode.java:306)

at org.python.core.PyBaseCode.call(PyBaseCode.java:126)

at org.python.core.PyFunction. **call** (PyFunction.java:416)

at org.python.pycode.\_pyx0.f$0(:78)

at org.python.pycode.\_pyx0.call\_function()

at org.python.core.PyTableCode.call(PyTableCode.java:173)

at org.python.core.PyCode.call(PyCode.java:18)

at org.python.core.Py.runCode(Py.java:1703)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:804)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:859)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:752)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:840)

at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:251)

at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:198)

at com.inductiveautomation.ignition.common.util.SerialExecutionQueue$PollAndExecute.run(SerialExecutionQueue.java:102)

at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.base/java.util.concurrent.FutureTask.run(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.base/java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: NameError: global name 'socket' is not defined

Also pls find the attached photot as well.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [February 27, 2025, 1:19am UTC](https://forum.inductiveautomation.com/t/regarding-gateway-event-script-tag-chabge/102950/6 "2025-02-27T01:19:53Z")

</div>

Please properly format the stack trace, see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).  
It makes it easier for others to read. You can edit your post by clicking the penicl icon in the bottom right.

As for the cause:

> [@Rakesh\_Parmar](#):
>
> global name 'socket' is not defined

It seems that its not happy with your use of `socket`

You should define this script in the scripting library of this project. There are some weird behaviors (most notable is the need to `import system`) with contexts in the event scripts.

These are almost always corrected by defining the script in the script library and calling the function from the event script.
