Skip to content
Snippets Groups Projects
Commit 820da608 authored by Rayane Houhou's avatar Rayane Houhou
Browse files

Added lost files from lab8 that were not saved in latest git commit, added...

Added lost files from lab8 that were not saved in latest git commit, added simpleServer to look at it more easily and fix the features I want in mySimpleServer
parent 6e48a7f1
No related merge requests found
"""
Define global config variables here.
These will be accessible from other
scripts, so we only need to set them
here.
"""
# app configurables stored in a config object
config = {
"SERVER_ADDRESS": "localhost",
"PORT": 27017,
"USER": "",
"PASS": "",
"DATABASE_NAME": "catflucks",
"TEMPLATE_DIR": "templates/"
}
#!/usr/bin/env python3
"""
This script handles the processing
of the login form in the catflucks application.
Note:
there is code repetition here...
What could we do about that?
Also note:
this functionality is not yet integrated
with the rest of the application.
How could it be?
"""
# import modules from Python standard library
import cgi
import cgitb
cgitb.enable()
import bcrypt
# import custom modules
from config import config
import utils
# connect to database
db = utils.db_connect( config )
# tell the browser we are outputting HTML
print("Content-Type: text/html\n")
# get the form data
form = cgi.FieldStorage()
# check that login form was submitted
if 'btn_login' in form:
# get the username from the form
username = form['username'].value
# look for an account with the username
account = db.accounts.find_one({
"username":username,
})
# check if an account came back
if account is not None:
# get the unhashed password sent in the form
pw = form['password'].value
# compare the unhashed password from the form
# with the hashed version in the database
if bcrypt.checkpw( pw.encode('utf-8'), account['password'].encode('utf-8') ):
# if they match, output a personal greeting
response = "Hello {}!".format( account['name']['first'] )
else:
# if not, we know it was a wrong password
response = "Wrong password."
else:
# their username didn't match, so tell them that
response = "Sorry, I don't think I know you!"
# for testing purposes, output the response here...
print("<p>"+response+"</p>")
else:
# they didn't come via the form, so tell them to go away!
print("<p>How did you get here? Maybe you want to <a href='/cgi-bin/splash.py'>go back home</a>?</p>")
#!/usr/bin/env python3
"""
This script is the main entry point to
the catflucks application.
"""
# import modules from Python standard library
from bson.objectid import ObjectId
import cgi
import cgitb
cgitb.enable()
from datetime import datetime
# import custom modules
from config import config
import utils
# connect to database
db = utils.db_connect( config )
# render header HTML
print("Content-Type: text/html\n")
print( utils.render_template( config['TEMPLATE_DIR'] + 'header.html') )
# render login form. Could use JS to collapse this by default.
print( utils.render_template( config['TEMPLATE_DIR'] + 'login.html') )
# get one random document from images collection
result = db.images.aggregate(
[{ '$sample': { 'size': 1 } }]
)
# if a result came back, do stuff with it...
if result:
# iterate through objects in the cursor (should only be 1)
for img in result:
# pull out the img url and alt text
img_src = img['url']
img_alt = img['alt']
img_id = img['_id']
# render serve_cat template, passing it dynamic data
print( utils.render_template( config['TEMPLATE_DIR']+'serve_cat.html', data=[img_src, img_alt] ) )
# find and count flucks with matching img_id and where is_flucked is 1
num_flucks = db.flucks.find( {"image_id": ObjectId(img_id), "is_flucked":1} ).count()
# render cat_stats template, passing it dynamic data
print( utils.render_template( config['TEMPLATE_DIR']+'cat_stats.html', data=[num_flucks] ) )
# render form_fluck template, passing it dynamic data
print( utils.render_template( config['TEMPLATE_DIR']+'form_fluck.html', data=["/cgi-bin/splash.py",img_id] ) )
else:
print("<p>Oops. Something went wrong!</p>")
# get form data
form = cgi.FieldStorage()
# check if either button clicked and insert a fluck in the database
if 'btn_fluck' in form:
result = db.flucks.insert( {
"image_id":ObjectId(form['img_id'].value),
"is_flucked":1,
"timestamp":datetime.now().timestamp()
} )
elif 'btn_skip' in form:
result = db.flucks.insert( {
"image_id":ObjectId(form['img_id'].value),
"is_flucked":0,
"timestamp":datetime.now().timestamp()
} )
# render cat_stats template, passing it the dynamic data
print( utils.render_template( config['TEMPLATE_DIR']+'footer.html' ) )
#!/usr/bin/env python3
# above 'she-bang' line makes the script executable from command line
""" A Simple Web Server
Run with ./simpleServer.py
Make sure all cgi scripts are executable
for single script:
chmod +x simpleServer.py
or for a whole directory:
chmod -r +x cgi-bin/
"""
import http.server # import http.server module
import cgitb; cgitb.enable() # import and enable cgitb module for exception handling
PORT = 8000 # specifies the port number to accept connections on
server = http.server.HTTPServer # provides simple web server
handler = http.server.CGIHTTPRequestHandler # provides request handler
server_address = ("", PORT) # specify server directory and port number
handler.cgi_directories = ["/","/cgi-bin","/htbin"] # where CGI scripts will reside in relation to the `server' directory
print("Starting server...") # outputs a message
httpd = server(server_address, handler) # creates the server, passing it the server address and port number, as well as the CGI handler (httpd stands for HTTP Daemon)
print("serving at port", PORT) # outputs a message
httpd.serve_forever() # puts program in infinite loop so that the server can `serve_forever'
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment