Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support python3 #21

Open
neszt opened this issue Sep 21, 2021 · 3 comments
Open

Support python3 #21

neszt opened this issue Sep 21, 2021 · 3 comments

Comments

@neszt
Copy link

neszt commented Sep 21, 2021

    import urllib2
ModuleNotFoundError: No module named 'urllib2'
@jdtak
Copy link

jdtak commented Dec 12, 2023

import urllib.request
import urllib.error

@neszt
Copy link
Author

neszt commented Dec 12, 2023

import urllib.request import urllib.error

It's not that simple:

Traceback (most recent call last):
   File "mattermost.py", line 114, in <module>
     response = request(args.url, payload(args))
   File "mattermost.py", line 107, in request
     req = urllib2.Request(url, data)
NameError: name 'urllib2' is not defined

However, i asked ChatGPT to convert it, here is the diff:

diff --git a/mattermost.py b/mattermost.py
index 627b3fd..160f00d 100755
--- a/mattermost.py
+++ b/mattermost.py
@@ -1,28 +1,10 @@
 #!/usr/bin/env python
 # coding: utf-8

 import argparse
 import json
-import urllib2
+import urllib.request
+from urllib.parse import quote

 VERSION = "0.3.1"

@@ -33,7 +15,7 @@ def parse():
     parser.add_argument('--username', help='Username to notify as',
                         default='Nagios')
     parser.add_argument('--iconurl', help='URL of icon to use for username',
-                        default='https://slack.global.ssl.fastly.net/7bf4/img/services/nagios_128.png') # noqa
+                        default='https://slack.global.ssl.fastly.net/7bf4/img/services/nagios_128.png')
     parser.add_argument('--notificationtype', help='Notification Type',
                         required=True)
     parser.add_argument('--hostalias', help='Host Alias', required=True)
@@ -53,13 +35,11 @@ def parse():
     args = parser.parse_args()
     return args

-
 def encode_special_characters(text):
     text = text.replace("%", "%25")
     text = text.replace("&", "%26")
     return text

-
 def emoji(notificationtype):
     return {
         "ACKNOWLEDGEMENT": ":thumbsup: ",
@@ -69,10 +49,9 @@ def emoji(notificationtype):
         "DOWNTIMEEND": ":sunny: "
     }.get(notificationtype, "")

-
 def text(args):
-    template_host = "__{notificationtype}__ {hostalias} is {hoststate}\n{hostoutput}" # noqa
-    template_service = "__{notificationtype}__ {hostalias} at {hostaddress}/{servicedesc} is {servicestate}\n{serviceoutput}" # noqa
+    template_host = "__{notificationtype}__ {hostalias} is {hoststate}\n{hostoutput}"
+    template_service = "__{notificationtype}__ {hostalias} at {hostaddress}/{servicedesc} is {servicestate}\n{serviceoutput}"
     if args.notificationtype == "ACKNOWLEDGEMENT":
         template_host += "\n{hostackcomment}\nacked by: {hostackauthor}"
         template_service += "\n{serviceackcomment}\nacked by: {serviceackauthor}"
@@ -87,7 +66,6 @@ def text(args):

     return encode_special_characters(text)

-
 def payload(args):
     payload = {
         "username": args.username,
@@ -101,14 +79,13 @@ def payload(args):
     data = "payload=" + json.dumps(payload)
     return data

-
 def request(url, data):
-    req = urllib2.Request(url, data)
-    response = urllib2.urlopen(req)
+    data = data.encode('utf-8')
+    req = urllib.request.Request(url, data)
+    response = urllib.request.urlopen(req)
     return response.read()

-
 if __name__ == "__main__":
     args = parse()
     response = request(args.url, payload(args))
-    print response
+    print(response.decode('utf-8'))

I tested and it is working in my environment.

@jdtak
Copy link

jdtak commented Dec 13, 2023

Good deal! I had to make the same changes to get it to work too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants