How do I start a website from scratch without the use of a framework?
I want to create a web service that communicates with a database and dynamically displays the contents based on a user's activity.
I want the appearance to be HTML5/CSS3.
I would prefer to communicate with the database through python, or go.
The database is Cassandra.
I feel like I have placed all of this effort into trying frameworks that have limiting options and run APIs where I have no clue what is really being done by the API.
I want a super simple example that I can understand the basics of two way communication. I want my /var/www to look like this :
# ls /var/www
An example of something as simple as an index.html: with two boxes, one that takes in text and submits it to a database and a second box that lists below the first the contents of that database.
And a second file, datawriteandread.py, to communicate to the database for adding new content and to display the old in the HTML.
What of my ignorance is showing in wanting something so simple?
Writing the python to Cassandra is easy enough, it's the communication and display in the HTML that I am lost on. I have programming chops but webdev is completely new to me.
preguntado el 24 de agosto de 12 a las 22:08
You can do this using CGI. A simple Python CGI script might be:
print "Content-type: text/html" print print "Hello, world!"
The details of setting up your web server to run a Python program as a CGI script will depend on your choice of web server. However, there should be a CGI section in the documentation that you can refer to.
cgi módulo has various functions that will be helpful for doing things like extracting form data submitted by a browser.
I think the most brain-dead, simple way to use Python would be as a Guión CGI. Your web server would simply run your Python script and redirect the output to the response. No frameworks, no nothing.
You can Google for "Pitón CGI" and get plenty of examples, such as esta. From there, you'd be able to interact with the database directly using the Python database driver of your choice. Your HTML would simply
POST data to your Python script directly.
This to configure Apache2:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all AddHandler cgi-script .py # tell Apache to handle every file with .py suffix as a cgi program AddHandler default-handler .html .htm # tell Apache to handle HTML files in regular way </Directory>
Y este fragmento de código:
http://wiki.python.org/moin/CgiScripts ...(thanks Mike Christensen for this link)
1 #!C:\Python27\python.exe -u 2 #!/usr/bin/env python 3 4 import cgi 5 import cgitb; cgitb.enable() # for troubleshooting 6 7 print "Content-type: text/html" 8 print 9 10 print """ 11 <html> 12 13 <head><title>Sample CGI Script</title></head> 14 15 <body> 16 17 <h3> Sample CGI Script </h3> 18 """ 19 20 form = cgi.FieldStorage() 21 message = form.getvalue("message", "(no message)") 22 23 print """ 24 25 <p>Previous message: %s</p> 26 27 <p>form 28 29 <form method="post" action="index.cgi"> 30 <p>message: <input type="text" name="message"/></p> 31 </form> 32 33 </body> 34 35 </html> 36 """ % cgi.escape(message)
Combine to give me exactly what I wanted sans the database, but the implementation is logical.
Here is the next step.
<HTML> <HEAD><TITLE>Login Page</TITLE></HEAD> <BODY> <CENTER> <FORM method="POST" action="http://yourserver/cgi-bin/login.py"> <paragraph> Enter your login name: <input type="text" name="login"> <paragraph> Enter your password: <input type=password name="password"> <paragraph> <input type="submit" value="Connect"> </FORM> </CENTER> <HR> </form> </BODY> </HTML>
Your login.py CGI:
#!/usr/local/bin/python import cgi def header(title): print "Content-type: text/html\n" print "<HTML>\n<HEAD>\n<TITLE>%s</TITLE>\n</HEAD>\n<BODY>\n" % (title) def footer(): print "</BODY></HTML>" form = cgi.FieldStorage() password = "python" if not form: header("Login Response") elif form.has_key("login") and form["login"].value != "" and form.has_key("password") and form["password"].value == password: header("Connected ...") print "<center><hr><H3>Welcome back," , form["login"].value, ".</H3><hr></center>" print r"""<form><input type="hidden" name="session" value="%s"></form>""" % (form["login"].value) print "<H3><a href=browse.html>Click here to start browsing</a></H3>" else: header("No success!") print "<H3>Please go back and enter a valid login.</H3>" footer()