webtools.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. import cgi, os, time
  3. form = cgi.FieldStorage()
  4. time_form = '%Y.%m.%d [%a] %H:%M'
  5. long_time = '%A, %B %d @ %H:%M'
  6. short_time = '%m-%d @ %H:%M'
  7. # print headers, receive CGI form info, create CGI
  8. # forms, basic cookie handling, and translate UNIX time
  9. # to the preferred timestamp format.
  10. def head(title=''):
  11. header = ["Content-type: text/html\n\n"]
  12. header.append("<title>{0}</title>".format(title))
  13. return "\n".join(header)
  14. def raw_query():
  15. return os.environ["QUERY_STRING"]
  16. def get_form(val):
  17. if form.getvalue(val):
  18. if len(form.getvalue(val)[0]) > 1:
  19. # print('\n'.join(form.getlist(val)))
  20. return cgi.escape('\n'.join(form.getlist(val))).strip()
  21. return cgi.escape(form.getvalue(val)).strip()
  22. else:
  23. return ''
  24. def put_form(ty='', na='', va='', re=''):
  25. inps = [ty, na, va]
  26. if ty != "textarea":
  27. exform = "<input type='{0}' name='{1}' value='{2}'>".format(*inps)
  28. else:
  29. exform = "<textarea name='{1}'>{2}</textarea>".format(*inps)
  30. if re:
  31. exform.replace(">", " required>")
  32. return exform
  33. def dropdown(na='', val=[], nam=[]):
  34. dropdown = ["<select name='{0}'>".format(na)]
  35. if len(val) > len(nam) or not nam:
  36. nam = val
  37. for n, i in enumerate(val):
  38. dropdown.append("<option value='{0}'>{1}</option>".format(val[n], \
  39. nam[n]))
  40. dropdown.append("</select>")
  41. return "\n".join(dropdown)
  42. def new_form(act='.', met='post'):
  43. formhead = "<form action='{0}' method='{1}'>".format(act, met)
  44. return formhead
  45. def put_cookie(name='', data=''):
  46. content = name+"="+data+";"
  47. c_string = "<meta http-equiv='set-cookie' content='{0}'>".format(content)
  48. return c_string
  49. def get_cookie():
  50. c_dict = {}
  51. if "HTTP_COOKIE" in os.environ:
  52. cookies = os.environ["HTTP_COOKIE"]
  53. cookies = cookies.split("; ")
  54. for c in cookies:
  55. c = c.split("=")
  56. c_dict[c[0]] = "=".join(c[1:])
  57. return c_dict
  58. def get_ip():
  59. return os.environ["REMOTE_ADDR"]
  60. def fancy_time(utime='', mode=''):
  61. if not utime:
  62. utime = int(time.time())
  63. else:
  64. utime = int(utime)
  65. if mode == 'unix':
  66. return str(utime)
  67. htime = time.localtime(utime)
  68. if mode == "human":
  69. htime = time.strftime(time_form, htime)
  70. return htime
  71. elif mode == "lt":
  72. return time.strftime(long_time, htime)
  73. elif mode == "st":
  74. return time.strftime(short_time, htime)
  75. else:
  76. htime = time.strftime(time_form, htime)
  77. return [utime, htime]
  78. def grab_html(fn=''):
  79. with open('./html/' + fn + '.html', 'r') as html:
  80. html = html.read()
  81. return html
  82. def redirect(sec=5, loc='.'):
  83. return "<meta http-equiv='refresh' content='" \
  84. + str(sec) + ";" + loc + "'>"