Форум сайта python.su
здравствуйте,подскажите пожалуйста как сделать кнопку средствами python в веб и обработать нажатие на эту кнопку.
кнопку и обработчик событий необходимо сделать в одном файле,например index.py
Офлайн
Если простенько то BaseHTTPServer подойдет:
http://docs.python.org/library/basehttpserver.html#module-BaseHTTPServer
http://blog.doughellmann.com/2007/12/pymotw-basehttpserver.html
http://wiki.python.org/moin/BaseHttpServer
…
import time
import BaseHTTPServer
import cgi
HOST_NAME = 'localhost'
PORT_NUMBER = 8001
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
form = '<b>Form</b><form action="" method="post"><input type="text" name="phone_number" id="id_phone_number" /><input type="submit" value="Do SOMETHING" /></form>'
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
"""Respond to a GET request."""
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("<html><head><title>FORM</title></head>")
self.wfile.write("<body><p></p>")
self.wfile.write(self.form)
self.wfile.write("</body></html>")
def do_POST(self):
# Parse the form data posted
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
# Begin the response
self.send_response(200)
self.end_headers()
self.wfile.write('Client: %s\n' % str(self.client_address))
self.wfile.write('Path: %s\n' % self.path)
self.wfile.write('Form data:\n')
# Echo back information about what was posted in the form
for field in form.keys():
field_item = form[field]
if field_item.filename:
# The field contains an uploaded file
file_data = field_item.file.read()
file_len = len(file_data)
del file_data
self.wfile.write('\tUploaded %s (%d bytes)\n' % (field,
file_len))
else:
# Regular form value
self.wfile.write('\t%s=%s\n' % (field, form[field].value))
return
if __name__ == '__main__':
server_class = BaseHTTPServer.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
Отредактировано (Май 30, 2011 12:19:13)
Офлайн