1

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

index.html datawriteandread.py

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.

Peregrine
  • 347
  • 4
  • 14

3 Answers3

0

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.

The Python cgi module has various functions that will be helpful for doing things like extracting form data submitted by a browser.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Probably not; there are many possible ways to integrate with a web server and CGI is the easiest and least complex (but has performance implications for busy sites). Another popular method for Python is WSGI. – Greg Hewgill Aug 24 '12 at 21:43
  • I think CGI is analogous to the web server just running an executable (python.exe in this case) and redirecting the standard output to the HTTP response; correct? – Mike Christensen Aug 24 '12 at 21:56
  • @MikeChristensen: It's slightly more than a simple redirection, since the HTTP server inserts its own HTTP status header if the CGI program doesn't supply one (though the CGI must supply the rest of the headers). Other than that, it's basically a straight-through pipe from the CGI to the browser. – Greg Hewgill Aug 25 '12 at 02:29
0

I think the most brain-dead, simple way to use Python would be as a CGI script. Your web server would simply run your Python script and redirect the output to the response. No frameworks, no nothing.

You can Google for "Python CGI" and get plenty of examples, such as this one. 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.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • What do you mean by brain-dead? I could be misreading it, but if I interpret what you mean correctly I guess I thought that the use of frameworks was the brain-dead option. It'll do the work for you while you learn some, other than intended use, useless abstraction layer? – Peregrine Aug 24 '12 at 21:41
  • I meant to exaggerate the simplicity of such an architecture. There's nothing between you and the raw HTTP response. No unnecessary levels of abstraction and no frameworks to download. Just you printing out exactly what you want the client to see. – Mike Christensen Aug 24 '12 at 21:44
  • If I find I like some other language better than Python do I just search out some other languages' CGI protocols? – Peregrine Aug 24 '12 at 22:00
  • Yea, CGI is a standard that most any web server supports. You can pretty much run any executable you want as a CGI script, as long as the standard output can be captured and redirected. I suggest reading the [Wikipedia](http://en.wikipedia.org/wiki/Common_Gateway_Interface) article for more information. – Mike Christensen Aug 24 '12 at 22:09
0

This to configure Apache2:

http://narnia.cs.ttu.edu/drupal/node/43 add

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>

to /etc/apache2/sites-available/default

And this code snippet:

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.

http://www.java2s.com/Tutorial/Python/0440__CGI-Web/Loginform.htm

Your index.htm:

<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()
Community
  • 1
  • 1
Peregrine
  • 347
  • 4
  • 14