I have a script that I want to use with mqtt however if I include the line:
import paho.mqtt.client as mqtt
the script fails. If I comment out the import the script runs…but of course is useless without mqtt. The script is below but here are some notes:
(1) This is a cgi script.
(2) You can see that I have commented out nearly everything to eliminate other possible problems.
(3) I can run this script using Geany and no errors are reported and it ends nicely.
(4) If I leave the import in and submit to run the script I get a “Internal Server Error…blah blah blah”. No useful information except the error log says, “End of script output before headers: Opener.cgi”, which is all over the internet without any solution.
(5) I have another cgi script that doesn’t use mqtt. If I insert the uncommented import, it fails also.
(6) I have many python scripts that use mqtt but are not used for cgi and they work fine.
I have exhausted my idea bucket. Any ideas?
#!/usr/bin/env python
import cgi, os.path, cgitb, time
#import paho.mqtt.client as mqtt
form=cgi.FieldStorage()
# def on_connect(client, userdata, rc):
# print("Connected with result code "+str(rc))
def main():
a=1
# print('Content-type: text/html\n\n')
# time.sleep(10)
print ('StartingMain')
# broker_address="192.168.1.115"
# client = mqtt.Client("P3")
# client.on_connect=on_connect('localhost',1883,60)
# print("connecting to broker")
# client.connect(broker_address,1883,60) #connect to broker
# change=form.getvalue("ChangeOverheadDoor")
# This pubs to a terminal window just fine client.publish("GarageCommand", "ON")
# print(change,"<br>")
# def fileToStr(fileName):
# """Return a string containing the contents of the named file."""
# fin = open(fileName);
# contents = fin.read();
# yesOrNo=fin.readline()
# forText=fin.readline()
# fin.close()
# return contents
# def strToFile(text, filename):
# """Write a file with the given name and the given text."""
# output = open(filename,"w")
# output.write(text)
# output.close()
# def browseLocal(webpageText, filename='/var/www/calinski.us/public_html/opener.html'):
# '''Start your webbrowser on a local file containing the text
# with given filename.'''
# import webbrowser, os.path
# strToFile(webpageText, filename)
# webbrowser.open("file:///" + os.path.abspath(filename)) #elaborated for Mac
try: # NEW
print("Content-type: text/html\n\n") # say generating html
main()
except:
cgi.print_exception() # catch and print errors