如何使用python BaseHTTPServer将html表单数据保存到sqlite数据库

时间:2018-08-14 06:17:08

标签: python html sqlite

我正在使用python BaseHTTPServer,它的代码是

#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from os import curdir, sep

PORT_NUMBER = 8080

#This class will handles any incoming request from
#the browser 
class myHandler(BaseHTTPRequestHandler):
        #Handler for the GET requests
    def do_GET(self):
        if self.path=="/":
            self.path="/index_example2.html"

        try:
            #Check the file extension required and
            #set the right mime type

            sendReply = False
            if self.path.endswith(".html"):
                mimetype='text/html'
                sendReply = True
            if self.path.endswith(".jpg"):
                mimetype='image/jpg'
                sendReply = True
            if self.path.endswith(".gif"):
                mimetype='image/gif'
                sendReply = True
            if self.path.endswith(".js"):
                mimetype='application/javascript'
                sendReply = True
            if self.path.endswith(".css"):
                mimetype='text/css'
                sendReply = True
            if self.path.endswith(".png"):
                mimetype='image/png'
                sendReply = True

            if sendReply == True:
                #Open the static file requested and send it
                f = open(curdir + sep + self.path) 
                self.send_response(200)
                self.send_header('Content-type',mimetype)
                self.end_headers()
                self.wfile.write(f.read())
                f.close()
            return


        except IOError:
            self.send_error(404,'File Not Found: %s' % self.path)

try:
    #Create a web server and define the handler to manage the
    #incoming request
    server = HTTPServer(('192.168.1.129', PORT_NUMBER), myHandler)
    print 'Started httpserver on port ' , PORT_NUMBER

    #Wait forever for incoming htto requests
    server.serve_forever()

except KeyboardInterrupt:
    print '^C received, shutting down the web server'
    server.socket.close()

使用此即时消息调用html代码

<!DOCTYPE html>
<!-- Template by html.am -->
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>BAT Box</title>
		<style type="text/css">
		
		body {
			margin: 0;
			padding: 0;
			overflow: hidden;
			height: 100%; 
			max-height: 100%; 
			font-family:Sans-serif;
			line-height: 1.5em;
		}
		
		#header {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100px; 
			overflow: hidden; /* Disables scrollbars on the header frame. To enable scrollbars, change "hidden" to "scroll" */
			background: #BCCE98;
		}
		
		#nav {
			position: absolute; 
			top: 100px; 
			left: 0; 
			bottom: 0;
			width: 230px;
			overflow: auto; /* Scrollbars will appear on this frame only when there's enough content to require scrolling. To disable scrollbars, change to "hidden", or use "scroll" to enable permanent scrollbars */
			background: #DAE9BC; 		
		}
		
		#logo {
			padding:10px;
		}
		
		main {
			position: fixed;
			top: 100px; /* Set this to the height of the header */
			left: 230px; 
			right: 0;
			bottom: 0;
			overflow: auto; 
			background: #fff;
		}
		
		.innertube {
			margin: 15px; /* Provides padding for the content */
		}
		
		p {
			color: #555;
		}

		nav ul {
			list-style-type: none;
			margin: 0;
			padding: 0;
		}
		
		nav ul a {
			color: darkgreen;
			text-decoration: none;
		}
				
		/*IE6 fix*/
		* html body{
			padding: 100px 0 0 230px; /* Set the first value to the height of the header and last value to the width of the nav */
		}
		
		* html main{ 
			height: 100%; 
			width: 100%; 
		}
		
		</style>
		
		<script type="text/javascript">
			/* =============================
			This script generates sample text for the body content. 
			You can remove this script and any reference to it. 
			 ============================= */
			
			function generateText(sentenceCount){
				for (var i=0; i<sentenceCount; i++)
				document.write(bodyText[Math.floor(Math.random()*7)]+" ")
			}
		</script>	
	
	</head>
	
	<body>		

		<header id="header">
			<div id="logo">
			<img src='image/logo.png' align="right" style="width:75px;height:50px;">
				<h1>CoolLogo</h1>
			</div>
		</header>
				
		<main>
			<div class="innertube">
				<fieldset style="width:400px; height: 150px; color:0FF;padding:2px;">
				
				<table style="background-color:#FFFFE0;">
					<form method="POST" action="/send">
					<tr>
						<td>IP Address</td><td><input type="text"/></td>
					</tr>
					<tr>
						<td>Net Mask</td><td><input type="text"/></td>
					</tr>
					<tr>
						<td>Gateway</td><td><input type="text"/></td>
					</tr>
					<tr>
						<td>MAC Address</td><td><input type="text"/></td>
					</tr>
					
				</table>
				
</fieldset>
					 
					<br>
				        <input type="submit" value="Submit" margin-right: 100px;/>

					<input type="submit" value="Reset"/>
				</div>
			</div>

		</main>

		<nav id="nav">
			<div class="innertube">
				<h1>Heading</h1>
				
					<form action="/login" method="POST">
				<ul>
					<label>Insert your name: </label><input type="text" name="your_name"/>
					<li><a href="index_example2.html">IP</a></li>
					<li><a href="index_example3.html">FSQ</a></li>
					<li><a href="index_example4.html">Server&FTP IP</a></li>
					<li><a href="index_example2.html">4</a></li>
					<li><a href="index_example2.html">5</a></li>
				</ul>
				</form>
			</div>
		</nav>	
	</body>
</html>

	<!--meta http-equiv="refresh" content="5"-->

1 个答案:

答案 0 :(得分:0)

  • 使用sqlite3.connect()方法连接到数据库
  • 运行sql CREATE TABLE(有关语法,请参见此处,使用IF NOT EXISTS处理已经存在的情况)
  • 定义发布方法处理程序
  • 为此使用parse_qs()解析表单数据
  • 使用表单中的name字段执行SQL INSERT语句。