diff --git a/.gitignore b/.gitignore index 7bbc71c..3921d07 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,6 @@ ENV/ # mypy .mypy_cache/ + +# DS_Store +*.DS_Store \ No newline at end of file diff --git a/setup.py b/setup.py index b15b377..2a59b18 100644 --- a/setup.py +++ b/setup.py @@ -3,13 +3,14 @@ setup( name="http-server", - description="A script that uses a client and server to echo a message.", + description="A concurrent web server.", version=0.1, author="Morgan, Ronel, Kurt", author_email="", license='MIT', py_modules=['client', 'server'], package_dir={'': 'src'}, + install_requires=['gevent'], extras_require={'test': ['pytest', 'pytest-watch', 'pytest-cov', 'tox']}, entry_points={ 'console_scripts': [ diff --git a/src/client.py b/src/client.py index 4f3225b..ecfb846 100644 --- a/src/client.py +++ b/src/client.py @@ -29,7 +29,7 @@ def client(message): return echo_message.decode('utf-8') -if __name__ == '__main__': +if __name__ == '__main__': # pragma no cover message = sys.argv[1] print(client(message)) sys.exit() diff --git a/src/server.py b/src/server.py index b951f4e..c5367d9 100644 --- a/src/server.py +++ b/src/server.py @@ -20,6 +20,7 @@ def resolve_uri(uri): if os.path.isdir(request_path): body = os.listdir(request_path) body = '
{}
'.format(body) + body = body.encode('utf-8') size = len(body) return 'text/html', body, size elif os.path.isfile(request_path): @@ -48,15 +49,17 @@ def resolve_uri(uri): def response_ok(uri): - """Send an ok response.""" - content_type, content, size = resolve_uri(uri) - response_ok = 'HTTP/1.1 200 OK\r\nContent-Type: {}\r\nContent-Length: {}{}{}{}'.format( - content_type, - size, - CRLF, - content, - CRLF) - return response_ok.encode('utf-8') + """Send a response OK, headers, and content.""" + content_type, content, content_size = resolve_uri(uri) + content_size = str(content_size) + msg = b'HTTP/1.1 200 OK\r\nContent-Type: ' + msg += content_type.encode('utf-8') + msg += b'\r\nContent-Length: ' + msg += content_size.encode('utf-8') + msg += CRLF.encode('utf-8') + msg += content + msg += CRLF.encode('utf-8') + return msg def response_error(error): @@ -135,5 +138,10 @@ def server(): sys.exit() -if __name__ == '__main__': - server() +if __name__ == '__main__': # pragma no cover + from gevent.server import StreamServer + from gevent.monkey import patch_all + patch_all() + server = StreamServer(('127.0.0.1', 5000), server) + print('Starting server on port 5000') + server.serve_forever() diff --git a/src/test_servers.py b/src/test_servers.py index 9d7b58b..3e98e08 100644 --- a/src/test_servers.py +++ b/src/test_servers.py @@ -7,7 +7,7 @@ CRLF = '\r\n' SUCCESS = 'HTTP/1.1 200 OK\r\n' -ROOT_LIST_RESP = SUCCESS + 'Content-Type: text/html\r\nContent-Length: 135\r\n\r\n[\'.DS_Store\', \'a_web_page.html\', \'favicon.ico\', \'images\', \'make_time.py\', \'sample.txt\']
\r\n\r\n' +ROOT_LIST_RESP = SUCCESS + 'Content-Type: text/html\r\nContent-Length: 107\r\n\r\n[\'a_web_page.html\', \'images\', \'make_time.py\', \'sample.txt\']
\r\n\r\n' IMAGES_LIST_RESP = SUCCESS + 'Content-Type: text/html\r\nContent-Length: 110\r\n\r\n[\'JPEG_example.jpg\', \'sample_1.png\', \'Sample_Scene_Balls.jpg\']
\r\n\r\n' WEB_PAGE_RESP = SUCCESS + 'Content-Type: text/html\r\nContent-Length: 125\r\n\r\n' @@ -60,7 +60,6 @@ HTTP_REQUEST_PARAMS_505 = [ 'GET /path/file.html HTTP/0.9\r\nHost: www.host1.com:80\r\n\r\n', - # 'GET / HTTP/1.0\r\nHost: www.host1.com:80\r\n\r\n', 'GET /path/file.html HTTP/0.9\r\nHost: www.host1.com:80\r\n\r\n' ] diff --git a/src/webroot/.DS_Store b/src/webroot/.DS_Store deleted file mode 100644 index f38c3dc..0000000 Binary files a/src/webroot/.DS_Store and /dev/null differ diff --git a/src/webroot/favicon.ico b/src/webroot/favicon.ico deleted file mode 100644 index 480b231..0000000 Binary files a/src/webroot/favicon.ico and /dev/null differ