Advertisement
Guest User

Untitled

a guest
Dec 5th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #! /usr/bin/env python3
  2. print("Content-type: text/html\n")
  3. import cgi
  4. import MySQLdb
  5. form = cgi.FieldStorage()
  6. string = "i211f18_affoster"
  7. password = "my+sql=i211f18_affoster"
  8. db_con = MySQLdb.connect(host = "db.soic.indiana.edu", port = 3306, user = string, passwd = password, db = string)
  9. cursor = db_con.cursor()
  10.  
  11. html = "<!doctype html>"
  12. html += "<html>"
  13. html += """<head><meta charset = "utf-8">"""
  14. html += "<title>Robot Delivery System Confirmation</title>"
  15. html += "<head>"
  16. html += "<body>"
  17. html += "<h1>Robot Delivery System Confirmation</h1>"
  18. html += "<br>"
  19. html += "<p>You have selected to have a {0} delivered by {1}.</p>"
  20. html += "<p>Your total comes out to ${2}</p>"
  21.  
  22. costs = {"drone":10, "car":20, "robot":1000, "nothing":0, "item wrong":0, "type wrong": 0}
  23. cost = form.getfirst("cost", 0)
  24. item = form.getfirst("item", "item wrong")
  25. type = form.getfirst("method", "type wrong")
  26.  
  27. html = html.format(item, type, str(int(cost) + costs[type]))
  28. html += "<h2>Delivery Records</h2><br>"
  29. try:
  30. sql = "insert into deliveries(item, cost, method, shipping) values ('" + item + "', " + str(cost) + ", '" + type + "', " + str(costs[type]) + ");"
  31. print(sql)
  32. cursor.execute(sql)
  33. db_con.commit()
  34. except:
  35. print("Something went wrong")
  36. else:
  37. sql = "select d.item, d.cost, d.method, d.shipping from deliveries as d;"
  38. cursor.execute(sql)
  39. results = cursor.fetchall()
  40. html += """<table border = 1><tr><th>Item</th><th>Cost</th><th>Method</th><th>Shipping</th></tr>"""
  41. for row in results:
  42. html += """<tr>"""
  43. for elem in row:
  44. html += "<td>" + str(elem) + "</td>"
  45. html += """</tr>"""
  46. html += "</table>"
  47. html += """</body>
  48. </html>"""
  49. print(html)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement