Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step3 #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ ENV/

# mypy
.mypy_cache/

# DS_Store
*.DS_Store
2 changes: 1 addition & 1 deletion src/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
23 changes: 13 additions & 10 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def resolve_uri(uri):
if os.path.isdir(request_path):
body = os.listdir(request_path)
body = '<!DOCTYPE html><html><body><p>{}</p></body></html>'.format(body)
body = body.encode('utf-8')
size = len(body)
return 'text/html', body, size
elif os.path.isfile(request_path):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -135,5 +138,5 @@ def server():
sys.exit()


if __name__ == '__main__':
if __name__ == '__main__': # pragma no cover
server()
3 changes: 1 addition & 2 deletions src/test_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<!DOCTYPE html><html><body><p>[\'.DS_Store\', \'a_web_page.html\', \'favicon.ico\', \'images\', \'make_time.py\', \'sample.txt\']</p></body></html>\r\n\r\n'
ROOT_LIST_RESP = SUCCESS + 'Content-Type: text/html\r\nContent-Length: 107\r\n\r\n<!DOCTYPE html><html><body><p>[\'a_web_page.html\', \'images\', \'make_time.py\', \'sample.txt\']</p></body></html>\r\n\r\n'
IMAGES_LIST_RESP = SUCCESS + 'Content-Type: text/html\r\nContent-Length: 110\r\n\r\n<!DOCTYPE html><html><body><p>[\'JPEG_example.jpg\', \'sample_1.png\', \'Sample_Scene_Balls.jpg\']</p></body></html>\r\n\r\n'
WEB_PAGE_RESP = SUCCESS + 'Content-Type: text/html\r\nContent-Length: 125\r\n\r\n'

Expand Down Expand Up @@ -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'
]

Expand Down
Binary file removed src/webroot/.DS_Store
Binary file not shown.
Binary file removed src/webroot/favicon.ico
Binary file not shown.