From 9888165ef27998c975aaa3c19b7290ff59e0a27c Mon Sep 17 00:00:00 2001 From: Morgan Nomura Date: Sun, 23 Jul 2017 23:34:44 -0700 Subject: [PATCH 1/7] fixed encoding issue so content is served properly --- src/server.py | 23 ++++++++++++++--------- src/test_servers.py | 3 +-- src/webroot/favicon.ico | Bin 18435 -> 0 bytes 3 files changed, 15 insertions(+), 11 deletions(-) delete mode 100644 src/webroot/favicon.ico diff --git a/src/server.py b/src/server.py index b951f4e..84a2470 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,18 @@ 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') + print(msg, type(msg), content, type(content)) + msg += content + msg += CRLF.encode('utf-8') + return msg def response_error(error): @@ -67,6 +71,7 @@ def response_error(error): '405': 'HTTP/1.1 405 Method Not Allowed', '505': 'HTTP/1.1 505 HTTP Version Not Supported' } + print('error', error, type(error)) response_error = '{}{}'.format(error_dict.get(error, 'HTTP/1.1 400 Bad Request'), CRLF) return response_error.encode('utf-8') diff --git a/src/test_servers.py b/src/test_servers.py index 5b1f30f..77fdaeb 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: 120\r\n\r\n

[\'.DS_Store\', \'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/favicon.ico b/src/webroot/favicon.ico deleted file mode 100644 index 480b2313f7e083da7dccec9078b7e31f693d41ca..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 18435 zcmeI4dsr0Z9>)hoC5*H(6gAyeGrSBtv-jO#33gFYR#q;CXv*yD>@uQPfs3vVaMJ6f5SGDNILN}ywQ-q{PxEK6gZ&gD7J@H`9r-rxKE zz3=;(_wsx9k6B=tFeW0re>em|5y@k9Y2a^f`V-b2{2tu&D+d1DV;?)&2|+y{q(33h zik1B!C`3(awFW~j<)WOqlwFXl)e7tm%1l}b2y&l$Z^;MayJFD1*QuH{%Idl4Yb)-l z=$5cH0d48or+Q<={zgOfKL)06tuJV<*O^b`49Hudi-aRTib{Dir(eGVv-jp8+lLY@cY>fY|5W*KVJ&^zj=u=Z;?P-pwmc9%-!0fu(h;z8m~-%L^68iwkb9Tpj(_ zU6xg)zqH)k;@(s;)9~@k0q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r#5TK^lVM*t{mwU9vEJ@UEHH_I=Sj zz30%fE@9B1(kpe3#T9c*OHaMq$wz_rKq$yv9E)Zr2F25cE Zo%}RvUPSz>k@O2@l9MLr-cQV#^H0UM^TGfC From 5cae76d688f4899f2e989c1c4a03a2af62d6e2d2 Mon Sep 17 00:00:00 2001 From: Morgan Nomura Date: Sun, 23 Jul 2017 23:36:56 -0700 Subject: [PATCH 2/7] removed print statements --- src/server.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/server.py b/src/server.py index 84a2470..5bdb532 100644 --- a/src/server.py +++ b/src/server.py @@ -57,7 +57,6 @@ def response_ok(uri): msg += b'\r\nContent-Length: ' msg += content_size.encode('utf-8') msg += CRLF.encode('utf-8') - print(msg, type(msg), content, type(content)) msg += content msg += CRLF.encode('utf-8') return msg @@ -71,7 +70,6 @@ def response_error(error): '405': 'HTTP/1.1 405 Method Not Allowed', '505': 'HTTP/1.1 505 HTTP Version Not Supported' } - print('error', error, type(error)) response_error = '{}{}'.format(error_dict.get(error, 'HTTP/1.1 400 Bad Request'), CRLF) return response_error.encode('utf-8') From 962c62e3de129fc4b14aee7f353f086aae0d4874 Mon Sep 17 00:00:00 2001 From: Morgan Nomura Date: Sun, 23 Jul 2017 23:40:22 -0700 Subject: [PATCH 3/7] updated gitignore, removed # DS_Store --- .gitignore | 3 +++ src/webroot/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 3 insertions(+) delete mode 100644 src/webroot/.DS_Store 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/src/webroot/.DS_Store b/src/webroot/.DS_Store deleted file mode 100644 index f38c3dc0cba2915b31ef7549a655027c607848b8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKOHRWu5SavdV8bmq0+--EyzvYYE)lB= z0h*EQH=bYOC&kVX5wFH}NwgrMIffvMG9hA~bX6974CGwnn(jB+#3yIq2N-w Date: Sun, 23 Jul 2017 23:42:50 -0700 Subject: [PATCH 4/7] added pragma no cover and updated test --- src/client.py | 2 +- src/server.py | 2 +- src/test_servers.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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 5bdb532..7f82fba 100644 --- a/src/server.py +++ b/src/server.py @@ -138,5 +138,5 @@ def server(): sys.exit() -if __name__ == '__main__': +if __name__ == '__main__': # pragma no cover server() diff --git a/src/test_servers.py b/src/test_servers.py index 5a77f3f..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: 120\r\n\r\n

[\'.DS_Store\', \'a_web_page.html\', \'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' From d93e86953399c7c760cd131be61fc68049646873 Mon Sep 17 00:00:00 2001 From: Morgan Nomura Date: Sun, 23 Jul 2017 23:51:38 -0700 Subject: [PATCH 5/7] carried step3 final updates to stream server --- src/server.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/server.py b/src/server.py index 7f82fba..e2b4650 100644 --- a/src/server.py +++ b/src/server.py @@ -139,4 +139,9 @@ def server(): if __name__ == '__main__': # pragma no cover - server() + from gevent.server import StreamServer + from gevent.monkey import patch_all + patch_all() + server = StreamServer(('127.0.0.1', 5000), el_server) + print('Starting server on port 5000') + server.serve_forever() From e2af7675cf05097092c908a270cb692505bfe2b1 Mon Sep 17 00:00:00 2001 From: Morgan Nomura Date: Sun, 23 Jul 2017 23:55:37 -0700 Subject: [PATCH 6/7] updated setup requirements --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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': [ From 9b4b0a1cfdc12dc55df9322bebf7b8d1cec4b0a0 Mon Sep 17 00:00:00 2001 From: Morgan Nomura Date: Mon, 24 Jul 2017 11:27:59 -0700 Subject: [PATCH 7/7] fixed typo --- src/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.py b/src/server.py index e2b4650..c5367d9 100644 --- a/src/server.py +++ b/src/server.py @@ -142,6 +142,6 @@ def server(): from gevent.server import StreamServer from gevent.monkey import patch_all patch_all() - server = StreamServer(('127.0.0.1', 5000), el_server) + server = StreamServer(('127.0.0.1', 5000), server) print('Starting server on port 5000') server.serve_forever()