# Python script error since the update to version 5

**URL:** <https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897>\
**Category:** Scripts (Python and JS)\
**Created:** [July 15, 2024, 12:14pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897 "2024-07-15T12:14:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![yblis](https://forum.seatable.com/user_avatar/forum.seatable.com/yblis/32/1604_2.png) [@yblis](https://forum.seatable.com/u/yblis)\
**Post date:** [July 15, 2024, 12:14pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/1 "2024-07-15T12:14:26Z")

</div>

Hello,

I migrated to the latest version of SeaTable self-hosted enterprise edition, version 5.

Since then, my Python scripts no longer work.

Is it because of the API update?

Thank you in advance.

## My Setup:

- SeaTable Edition: Enterprise
- SeaTable Version: 5.0.7

## Description of the Problem:

I migrated to the latest version of SeaTable self-hosted enterprise edition, version 5.

Since then, my Python scripts no longer work.

Is it because of the API update?

## Error Messages:

Here is my Python script that sends an email with data from certain columns of my table:

```python
import smtplib
from urllib.parse import unquote
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.header import Header
from seatable_api import Base, context
import os
import markdown
import base64
import re

# Initializing the SeaTable API
base = Base(context.api_token, context.server_url)
base.auth()

# Retrieving the active row
active_row = context.current_row
if not active_row:
    print("No active row detected.")
    exit()

# Email parameters
sender = "Client.VPN@gifi.fr"
subject = active_row.get('Sujet', 'Default Subject')

# Email recipient
receiver_email = active_row.get('Mail')
if not receiver_email:
    print("Email address missing in the active row.")
    exit()

# Preparing the message
msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8').encode()
msg['From'] = sender
msg['To'] = receiver_email

# Email body
markdown_content = active_row.get('Contenue Mail', 'Default content if not found')
html_content = markdown.markdown(markdown_content)

# Function to download and encode images in base64
def download_and_encode_image(url):
    save_path = "/tmp/" + unquote(url.split('/')[-1])
    base.download_file(url, save_path)
    with open(save_path, "rb") as img_file:
        b64_string = base64.b64encode(img_file.read()).decode('utf-8')
    os.remove(save_path)
    return b64_string

# Replacing image URLs with base64 encoded images in the HTML content
image_urls = re.findall(r'!\[.*?\]\((.*?)\)', markdown_content)
for url in image_urls:
    b64_string = download_and_encode_image(url)
    html_content = html_content.replace(url, f"data:image/png;base64,{b64_string}")

msg.attach(MIMEText(html_content, 'html', 'utf-8'))

# Creating the attachment from the "Config VPN" column
config_vpn_content = active_row.get('Config VPN', '')
user = active_row.get('User', 'unknown')
attachment_filename = f"ClientVPN_{user}.vpn"
attachment_path = f"/tmp/{attachment_filename}"

with open(attachment_path, 'w', encoding='utf-8') as f:
    f.write(config_vpn_content)

with open(attachment_path, 'rb') as f:
    attachment = MIMEApplication(f.read())
    attachment.add_header('Content-Disposition', 'attachment', filename=attachment_filename)
    msg.attach(attachment)

os.remove(attachment_path)

# Sending the email and updating the columns based on the result
try:
    with smtplib.SMTP('myservermailhost', 25) as server:
        server.sendmail(sender, receiver_email, msg.as_string())
    base.update_row(context.current_table, active_row['_id'], {"Envoi email": True, "Logs envoie de mail": "Email sent successfully."})
    print("Email sent successfully.")
except Exception as e:
    log_message = f"Error sending email: {str(e)}"
    base.update_row(context.current_table, active_row['_id'], {"Envoi email": False, "Logs envoie de mail": log_message})
    print(log_message)

```

Here are the logs:

```python
Traceback (most recent call last):
File "/scripts/index.py", line 80, in <module>
base.update_row(context.current_table, active_row['_id'], {"Envoi email": True, "Logs envoie de mail": "Email sent successfully."})
File "/root/.local/lib/python3.11/site-packages/seatable_api/main.py", line 26, in wrapper
return func(obj, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/main.py", line 33, in wrapper
return getattr(new_obj, func. __name__ )(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/api_gateway.py", line 305, in update_row
return parse_response(response)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/utils.py", line 213, in parse_response
raise ConnectionError(response.status_code, response.text)
ConnectionError: [Errno 502] <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/scripts/index.py", line 84, in <module>
base.update_row(context.current_table, active_row['_id'], {"Envoi email": False, "Logs envoie de mail": log_message})
File "/root/.local/lib/python3.11/site-packages/seatable_api/main.py", line 26, in wrapper
return func(obj, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/main.py", line 33, in wrapper
return getattr(new_obj, func. __name__ )(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/api_gateway.py", line 305, in update_row
return parse_response(response)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/utils.py", line 213, in parse_response
raise ConnectionError(response.status_code, response.text)
ConnectionError: [Errno 502] <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>

```

This script works on version 4.

---

<div class="post-metadata">

**Author:** ![cdb](https://forum.seatable.com/user_avatar/forum.seatable.com/cdb/32/7_2.png) [@cdb](https://forum.seatable.com/u/cdb)\
**Post date:** [July 15, 2024, 12:44pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/2 "2024-07-15T12:44:30Z")

</div>

Did you configure the api-gateway in your nginx.conf?

---

<div class="post-metadata">

**Author:** ![yblis](https://forum.seatable.com/user_avatar/forum.seatable.com/yblis/32/1604_2.png) [@yblis](https://forum.seatable.com/u/yblis)\
**Post date:** [July 15, 2024, 12:49pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/3 "2024-07-15T12:49:13Z")

</div>

No, I haven’t made any changes to my `nginx.conf`. What changes should I make, please?

---

<div class="post-metadata">

**Author:** ![cdb](https://forum.seatable.com/user_avatar/forum.seatable.com/cdb/32/7_2.png) [@cdb](https://forum.seatable.com/u/cdb)\
**Post date:** [July 15, 2024, 1:57pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/4 "2024-07-15T13:57:07Z")

</div>

Please look here: [Extra Upgrade Notices - SeaTable Admin Manual](https://admin.seatable.io/upgrade/extra-upgrade-notice/)

 ![image](https://forum.seatable.io/uploads/default/original/2X/b/bcfe6040a26f57782dfa46899ddb058ecbf8896c.png)

---

<div class="post-metadata">

**Author:** ![yblis](https://forum.seatable.com/user_avatar/forum.seatable.com/yblis/32/1604_2.png) [@yblis](https://forum.seatable.com/u/yblis)\
**Post date:** [July 15, 2024, 2:22pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/5 "2024-07-15T14:22:09Z")

</div>

I just checked, and I already have this section…

---

<div class="post-metadata">

**Author:** ![cdb](https://forum.seatable.com/user_avatar/forum.seatable.com/cdb/32/7_2.png) [@cdb](https://forum.seatable.com/u/cdb)\
**Post date:** [July 15, 2024, 4:14pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/6 "2024-07-15T16:14:53Z")

</div>

Which version of the python-runner do you use? You have built your own docker container, right?

---

<div class="post-metadata">

**Author:** ![yblis](https://forum.seatable.com/user_avatar/forum.seatable.com/yblis/32/1604_2.png) [@yblis](https://forum.seatable.com/u/yblis)\
**Post date:** [July 15, 2024, 5:46pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/7 "2024-07-15T17:46:02Z")

</div>

I use this version of the python-runner: `seatable/seatable-python-runner:latest` deployed via a Docker stack.

What’s surprising is that it was working perfectly before the migration to version 5, which is why I was asking if anything had changed on that side?

---

<div class="post-metadata">

**Author:** ![yblis](https://forum.seatable.com/user_avatar/forum.seatable.com/yblis/32/1604_2.png) [@yblis](https://forum.seatable.com/u/yblis)\
**Post date:** [July 15, 2024, 7:09pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/8 "2024-07-15T19:09:14Z")

</div>

Another piece of information: when I look at the browser console, I see this:

 ![image](https://forum.seatable.io/uploads/default/original/2X/9/981e8110ee72448b9fd6763edbf6bdabcb9e8c4d.png)

---

<div class="post-metadata">

**Author:** ![cdb](https://forum.seatable.com/user_avatar/forum.seatable.com/cdb/32/7_2.png) [@cdb](https://forum.seatable.com/u/cdb)\
**Post date:** [July 16, 2024, 5:35am UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/9 "2024-07-16T05:35:56Z")

</div>

Give me some time to check this.

---

<div class="post-metadata">

**Author:** ![yblis](https://forum.seatable.com/user_avatar/forum.seatable.com/yblis/32/1604_2.png) [@yblis](https://forum.seatable.com/u/yblis)\
**Post date:** [July 16, 2024, 7:33am UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/10 "2024-07-16T07:33:50Z")

</div>

Additional info: the email is sent via my script, but the row update in the table does not work.

---

<div class="post-metadata">

**Author:** ![daniel.pan](https://forum.seatable.com/user_avatar/forum.seatable.com/daniel.pan/32/13_2.png) [@daniel.pan](https://forum.seatable.com/u/daniel.pan)\
**Post date:** [July 16, 2024, 10:14am UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/11 "2024-07-16T10:14:22Z")

</div>

502 error usually means that API-gateway does not start correctly in your server. You can check logs/dtable-api-gateway.log for error logs.

---

<div class="post-metadata">

**Author:** ![yblis](https://forum.seatable.com/user_avatar/forum.seatable.com/yblis/32/1604_2.png) [@yblis](https://forum.seatable.com/u/yblis)\
**Post date:** [July 16, 2024, 12:49pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/12 "2024-07-16T12:49:06Z")

</div>

I found the problem. It was due to a misconfiguration in the `dtable_server_config.json` file.

Indeed, the Redis host was incorrect…

Once I corrected it and recreated the containers, it worked!

Thank you for your help 🙂

---

<div class="post-metadata">

**Author:** ![system](https://forum.seatable.com/uploads/default/original/1X/04603423fe81a46c321283663086c475429e8ccb.png) [@system](https://forum.seatable.com/u/system)\
**Post date:** [July 18, 2024, 12:49pm UTC](https://forum.seatable.com/t/python-script-error-since-the-update-to-version-5/4897/13 "2024-07-18T12:49:38Z")

</div>

Closed on July 18, 2024, 12:49pm UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
