Skip to content

Commit

Permalink
did hw8 and Project option #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Taylor Jones committed Mar 20, 2014
1 parent 666873f commit 649fbe9
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 45 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@
* Branched test file and added test functions for each case
* Divided code into functions and added features to functions
* Handled POST and GET requests

2014-03-19 Taylor Jones

* Removed render.py
* Fixed href tag in images.html
* Updated arguments in test_server.py main() function
* Project Option 5: Added images_thumb with thumbnails. Also added a URL that provides a list of images, resized to thumbnails (50x50).
6 changes: 6 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __call__(self, environ, start_response):
'/content' : self.content,
'/file' : self.File,
'/image' : self.Image,
'/images_thumb' : self.images_thumb,
'/form' : self.form,
'/submit' : self.submit }

Expand Down Expand Up @@ -73,6 +74,11 @@ def Image(self, environ, start_response):
params = dict(names=get_contents('images'))
return render_page('image.html', params)

def images_thumb(self, environ, start_response):
start_response('200 OK', [('Content-type', 'text/html')])
params = dict(names=get_contents('images'))
return render_page('images_thumb.html', params)

def form(self, environ, start_response):
start_response('200 OK', [('Content-type', 'text/html')])
return render_page('form.html','')
Expand Down
80 changes: 66 additions & 14 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env python
import argparse
from app import make_app
import imageapp
import quixote
from quixote.demo.altdemo import create_publisher
import random
import socket
from StringIO import StringIO
Expand Down Expand Up @@ -41,7 +45,7 @@ def handle_connection(conn, port):
env['wsgi.multiprocess'] = False
env['wsgi.run_once'] = False
env['wsgi.url_scheme'] = 'http'
env['HTTP_COOKIE'] = headers['cookie']
#env['HTTP_COOKIE'] = headers['cookie']

body = ''
if request.startswith('POST '):
Expand All @@ -68,26 +72,74 @@ def start_response(status, response_headers):
conn.send(data)
conn.close()

def get_args():
app_list = ['altdemo', 'image', 'myapp']
parser = argparse.ArgumentParser()
parser.add_argument('-A', action="store",
dest='arg_app',
help="The application to run")
parser.add_argument('-p', action="store",
default=0,
dest='arg_port',
help="The port to use (optional)",
required=False,
type=int)

results = parser.parse_args()
if results.arg_app not in app_list:
print '\nError, that application does not exist\n'
exit()
return results.arg_app, results.arg_port

def main(socketmodule=None):
if socketmodule is None:
socketmodule = socket

s = socketmodule.socket() # Create a socket object
host = socketmodule.getfqdn() # Get local machine name
port = random.randint(8000, 9999)
s.bind((host, port)) # Bind to the port
app, port = get_args()

print 'Starting server on', host, port
print 'The Web server URL for this would be http://%s:%d/' % (host, port)
if app == 'myapp':
s = socketmodule.socket()
host = socketmodule.getfqdn()
if port == 0:
port = random.randint(8000, 9999)
s.bind((host, port))
print 'Starting server on', host, port
print 'The Web server URL for this would be http://%s:%d/' % (host, port)
s.listen(5)
print 'Entering infinite loop; hit CTRL-C to exit'
while True:
c, (client_host, client_port) = s.accept()
print 'Got connection from', client_host, client_port
handle_connection(c, client_port)

s.listen(5) # Now wait for client connection
elif app == 'image':
imageapp.setup()
p = imageapp.create_publisher()
wsgi_app = quixote.get_wsgi_app()
from wsgiref.simple_server import make_server
host = socketmodule.getfqdn()
if port == 0:
port = random.randint(8000, 9999)
httpd = make_server('', port, wsgi_app)
print 'Starting server on', host, port
print 'The Web server URL for this would be http://%s:%d/' % (host, port)
try:
httpd.serve_forever()
finally:
imageapp.teardown()

print 'Entering infinite loop; hit CTRL-C to exit'
while True:
# Establish connection with client
c, (client_host, client_port) = s.accept()
print 'Got connection from', client_host, client_port
handle_connection(c, client_port)
elif app == 'altdemo':
p = create_publisher()
wsgi_app = quixote.get_wsgi_app()
from wsgiref.simple_server import make_server
host = socketmodule.getfqdn()
if port == 0:
port = random.randint(8000, 9999)
p.is_thread_safe = True
httpd = make_server('', port, wsgi_app)
print 'Starting server on', host, port
print 'The Web server URL for this would be http://%s:%d/' % (host, port)
httpd.serve_forever()

if __name__ == '__main__':
main()
9 changes: 7 additions & 2 deletions templates/404.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{% extends "base.html" %}

{% block content %}
{% block title %} Web Server - Error Page {% endblock title %}

{% block body %}

<h1>Error Page</h1>
Oopsies, this isn't the page you want. :(

{% endblock %}
{% endblock body %}


18 changes: 13 additions & 5 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<!DOCTYPE html>
<html>
<body>
{% block content %}
{% endblock %}
</body>
</html>

<head>
{% block head %}
<title> {% block title %} {% endblock title %} </title>
{% endblock head %}
</head>

<body>
{% block body %}
{% endblock body %}
</body>
</html>
20 changes: 12 additions & 8 deletions templates/image.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{% extends "base.html" %}
{% block content %}
<h1>Image Page</h1>
This page contains images.<br />
<ul>

{% block title %} Web Server - Images Page {% endblock title %}

{% block body %}

<h1>Images Page</h1>

<ul>
{% for name in names %}
<a href='/pics/{{ name }}'>{{ name }}<br>
<a href='/pics/{{ name }}'>{{ name }}</a><br>
{% endfor %}
</ul>
<a href="/">Home</a>
{% endblock %}
</ul>

{% endblock body %}
24 changes: 13 additions & 11 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{% extends "base.html" %}
{% block content %}
<h1>Hello, world!</h1>
This is jonest31's web server.<br />
<h2>Home</h2>
<ul>
<li><a href="/content">Content</a></li>
<li><a href="/file">File</a></li>
<li><a href="/image">Image</a></li>
<li><a href="/form">Form</a></li>
</ul>
{% endblock %}

{% block body %}

<h1>Hello World!</h1>
This is jonest31's Web server.
<p>
<a href='/content'>Content</a><br>
<a href='/files'>Files</a><br>
<a href='/images'>Images</a><br>
<a href='/images_thumb'>Thumbnail Images</a><br>
<a href='/form'>Form</a>

{% endblock body %}
25 changes: 20 additions & 5 deletions test_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! /usr/bin/env python
import server
import sys

def test_error():
conn = FakeConnection("GET /404NotFound HTTP/1.0\r\n\r\n")
Expand Down Expand Up @@ -59,6 +60,18 @@ def test_Image():
else:
pass

def test_images_thumb():
conn = FakeConnection("GET /images_thumb HTTP/1.0\r\n\r\n")
server.handle_connection(conn, 80)
result = conn.sent

if ('HTTP/1.0 200 OK' and \
'Content-type: text/html' and \
'Thumbnail Images Page') not in result:
assert False
else:
pass

def test_form():
conn = FakeConnection("GET /form HTTP/1.0\r\n\r\n")
server.handle_connection(conn, 80)
Expand All @@ -76,12 +89,12 @@ def test_form():
pass

def test_submit():
conn = FakeConnection("GET /submit?firstname=Eunbong&lastname=Yang&submit=Submit HTTP/1.0\r\n\r\n")
conn = FakeConnection("GET /submit?firstname=Taylor&lastname=Jones&submit=Submit HTTP/1.0\r\n\r\n")
server.handle_connection(conn, 80)
result = conn.sent

if ('HTTP/1.0 200 OK' and \
'Hello Eunbong Yang!') not in result:
'Hello Taylor Jones!') not in result:
assert False
else:
pass
Expand All @@ -90,7 +103,7 @@ def test_post_app():
conn = FakeConnection("POST /submit HTTP/1.0\r\n" + \
"Content-Length: 31\r\n" + \
"Content-Type: application/x-www-form-urlencoded\r\n\r\n" + \
"firstname=Eunbong&lastname=Yang\r\n")
"firstname=Taylor&lastname=Jones\r\n")
server.handle_connection(conn, 80)
result = conn.sent

Expand All @@ -106,11 +119,11 @@ def test_post_multi():
"--AaB03x\r\n" + \
"Content-Disposition: form-data; name=\"firstname\";" + \
" filename=\"firstname\"\r\n\r\n" + \
"Eunbong\r\n" + \
"Taylor\r\n" + \
"--AaB03x\r\n" + \
"Content-Disposition: form-data; name=\"lastname\";" + \
" filename=\"lastname\"\r\n\r\n" + \
"Yang\r\n" + \
"Jones\r\n" + \
"--AaB03x\r\n" + \
"Content-Disposition: form-data; name=\"key\";" + \
" filename=\"key\"\r\n\r\n" + \
Expand All @@ -126,6 +139,8 @@ def test_post_multi():

def test_main():
fakemodule = FakeSocketModule()
sys.argv[1] = '-A'
sys.argv.append('myapp')

success = False
try:
Expand Down

0 comments on commit 649fbe9

Please sign in to comment.