Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. prodicus@Acer:~/Downloads/souvik_refactoring$ tree
  2. .
  3. ├── cgi-bin
  4. │   ├── creating_user_base_table.py
  5. │   ├── user_base.db
  6. │   └── usr_check.py
  7. ├── index.html
  8. └── keyCheck.py
  9.  
  10. 1 directory, 5 files
  11.  
  12. <!DOCTYPE html>
  13. <html>
  14. <head>
  15. <title>Login page</title>
  16. </head>
  17. <body>
  18. <div style = "text-align : center ; ">
  19. <h1>Login page</h1>
  20. <form action="/cgi-bin/usr_check.py" method="get">
  21. Registration number : <input type="number" name="register_no" min="1" max="2000000000">
  22. <br><br>
  23. Username : <input type="text" name="username">
  24. <br><br>
  25. Password : <input type="password" name = "password">
  26. <br><br>
  27. <input type="submit" value = "Login">
  28. </form>
  29. </div>
  30. </body>
  31. </html>
  32.  
  33. #!/usr/bin/env python3.4
  34.  
  35. import sqlite3
  36. import os
  37.  
  38. db_name = "user_base.db"
  39.  
  40. if db_name in os.listdir():
  41. print("removing the user_base.db and creating a fresh copy of it")
  42. os.system("rm user_base.db")
  43.  
  44. print("Creating the database")
  45. conn = sqlite3.connect(db_name)
  46. cur = conn.cursor()
  47.  
  48. user_table = "CREATE TABLE users(reg_no INTEGER PRIMARY KEY, user_name TEXT, pass TEXT)"
  49.  
  50. new_users = (
  51. (1081310251, 'admin', 'admin'),
  52. (1081310234, 'foo', 'admin123')
  53. )
  54.  
  55. cur.execute(user_table)
  56. print("table created")
  57. cur.executemany('INSERT INTO users VALUES(?, ?, ?)', new_users)
  58. conn.commit()
  59. print("default users created nndisplaying them")
  60.  
  61. cur.execute('SELECT * FROM users')
  62. print(cur.fetchall())
  63.  
  64. #/usr/bin/env python3.4
  65.  
  66. import cgi, cgitb
  67. import os
  68. import sqlite3
  69.  
  70. cgitb.enable()
  71.  
  72. form = cgi.FieldStorage()
  73. register_no = form.getvalue('register_no')
  74. username = form.getvalue('username')
  75. passwd = form.getvalue('password')
  76.  
  77. print("Content-type:text/htmlrnrn")
  78. print("<html>")
  79. print("<head>")
  80. print("<h1>Shit gets real here</h1>")
  81. print("</head>")
  82. print("<body>")
  83. print('<div style = "text-align:center ; "')
  84. # print("</div>")
  85. print("")
  86.  
  87. conn = sqlite3.connect('user_base.db')
  88. cur = conn.cursor()
  89.  
  90. ## now to check whether the entered data is for
  91. ## -> new user
  92. ## -> an old user
  93.  
  94. cur.execute('SELECT user_name FROM users WHERE register_no = ?', (register_no,))
  95. rows = cur.fetchall()
  96. print("<br><br>")
  97. if len(rows) == 0:
  98. print("<p>User : <b>", username , "</b> does not exist.</p>")
  99. cur.execute('INSERT INTO users VALUES(?, ?, ?)', (register_no, username, passwd))
  100. print("<p>User was created successfully</p>")
  101. print("Done")
  102.  
  103. else:
  104. print("<p>Welcome<b>", username ,"</b>. Good to have you back")
  105. print("<br><p>Your account details</p>")
  106. print("<ul>")
  107. print("<li>Register number : ", register_no, " </li>")
  108. print("<li>Username " , username, "</li>")
  109. print("</ul>")
  110.  
  111. prodicus@Acer:~/Downloads/souvik_refactoring$ python -m CGIHTTPServer
  112. Serving HTTP on 0.0.0.0 port 8000 ...
  113. 127.0.0.1 - - [02/Nov/2015 12:43:23] "GET /index.html HTTP/1.1" 200 -
  114. 127.0.0.1 - - [02/Nov/2015 12:44:03] "GET /cgi-bin/usr_check.py?register_no=1081310234&username=foo&password=admin123 HTTP/1.1" 200 -
  115. Traceback (most recent call last):
  116. File "/usr/lib/python2.7/CGIHTTPServer.py", line 252, in run_cgi
  117. os.execve(scriptfile, args, env)
  118. OSError: [Errno 8] Exec format error
  119. 127.0.0.1 - - [02/Nov/2015 12:44:03] CGI script exit status 0x7f00
  120.  
  121. prodicus@Acer:~/Downloads/souvik_refactoring$ ll
  122. total 36
  123. drwxrwxr-x 3 prodicus prodicus 4096 Nov 2 08:30 ./
  124. drwxr-xr-x 15 prodicus prodicus 20480 Nov 2 11:29 ../
  125. drwxrwxrwx 2 prodicus prodicus 4096 Nov 2 12:23 cgi-bin/
  126. -rw-rw-r-- 1 prodicus prodicus 629 Nov 2 08:38 index.html
  127. -rwxrwxr-x 1 prodicus prodicus 463 Nov 2 08:29 keyCheck.py*
  128.  
  129. prodicus@Acer:~/Downloads/souvik_refactoring/cgi-bin$ ll
  130. total 20
  131. drwxrwxrwx 2 prodicus prodicus 4096 Nov 2 12:23 ./
  132. drwxrwxr-x 3 prodicus prodicus 4096 Nov 2 08:30 ../
  133. -rwxrwxrwx 1 prodicus prodicus 710 Nov 1 23:12 creating_user_base_table.py*
  134. -rwxrwxrwx 1 prodicus prodicus 2048 Nov 2 12:23 user_base.db*
  135. -rwxrwxrwx 1 prodicus prodicus 1576 Nov 2 08:26 usr_check.py*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement