maze_cgi.html


001| #! C:\Python310\python.exe
002| ####################################################################################################
003| from sys import stdout, path as syspath
004| from os import path
005| from datetime import datetime
006| from io import BytesIO
007| from cgi import FieldStorage
008| ##\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\##
009| syspath.append(r'C:\CJS\prj\Python\app\maze')
010| ##////////////////////////////////////////////////////////////////////////////////////////////////##
011| from maze import MazeObject, MazeImagePng as MazeImage, MazeGen1
012| ####################################################################################################
013| BasePath = r'C:\CJS\prj\Python\app\maze'
014| 
015| if __name__ == '__main__':
016|     dt = datetime.today()
017|     Form = FieldStorage()
018| 
019|     Cmd = Form['cmd'].value if 'cmd' in Form else ''
020|     Alt = Form['alt'].value if 'alt' in Form else ''
021|     Rows = int(Form['rows'].value) if 'rows' in Form else 10
022|     Cols = int(Form['cols'].value) if 'cols' in Form else 10
023| 
024|     try:
025|         # Create a Maze...
026|         mz = MazeObject(Rows, Cols)
027|         mg = MazeGen1(mz)
028|         mg.generate_maze()
029| 
030|         # Entry and Exits...
031|         mz.set_entry_and_exit()
032| 
033|         # Create an image of the maze...
034|         mi = MazeImage(mz)
035|         mi.draw_maze()
036| 
037|         # 'Save' image to a string...
038|         output = BytesIO()
039|         mi.save(output, 'GIF')
040|         content = output.getvalue()
041|         output.close()
042|         mi.save(path.join(BasePath,'maze.gif'))
043| 
044|         # Emit the Image as an http response...
045|         stdout.write('Content-Type: image/gif\n')
046|         stdout.write('Content-Length: %d\n' % len(content))
047|         stdout.write('Last-Modified: %s\n' % dt.ctime())
048|         stdout.write('Pragma: no-cache\n')
049|         stdout.write('Reply-To: developer@sonnack.com\n')
050|         stdout.write('Errors-To: developer@sonnack.com\n')
051|         stdout.write('\n')
052|         stdout.flush()
053|         stdout.buffer.write(content)
054|         stdout.flush()
055| 
056|     except Exception as e:
057|         content = str(e)
058|         stdout.write('Content-Type: html/text\n')
059|         stdout.write('Content-Length: %d\n' % len(content))
060|         stdout.write('Last-Modified: %s\n' % dt.ctime())
061|         stdout.write('Pragma: no-cache\n')
062|         stdout.write('Reply-To: developer@sonnack.com\n')
063|         stdout.write('Errors-To: developer@sonnack.com\n')
064|         stdout.write('\n')
065|         stdout.write('<html><head>\n')
066|         stdout.write('<title>ERROR!</title>\n')
067|         stdout.write('</head><body>\n')
068|         stdout.write('<h1>ERROR!</h1>\n')
069|         stdout.write('<p>%s</p>\n' % content)
070|         stdout.write('</body></html>\n')
071|         stdout.flush()
072| 
073| ####################################################################################################
074| '''eof'''
075|