-
Notifications
You must be signed in to change notification settings - Fork 4
Sourcery refactored master branch #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,19 +44,18 @@ def size_str(bytes): | |
| bytes = float(bytes) | ||
| if bytes >= 1099511627776: | ||
| terabytes = bytes / 1099511627776 | ||
| size = '%.2f T' % terabytes | ||
| return '%.2f T' % terabytes | ||
| elif bytes >= 1073741824: | ||
| gigabytes = bytes / 1073741824 | ||
| size = '%.2f G' % gigabytes | ||
| return '%.2f G' % gigabytes | ||
| elif bytes >= 1048576: | ||
| megabytes = bytes / 1048576 | ||
| size = '%.2f M' % megabytes | ||
| return '%.2f M' % megabytes | ||
| elif bytes >= 1024: | ||
| kilobytes = bytes / 1024 | ||
| size = '%.2f K' % kilobytes | ||
| return '%.2f K' % kilobytes | ||
| else: | ||
| size = '%d B' % int(bytes) | ||
| return size | ||
| return '%d B' % int(bytes) | ||
|
|
||
| def time_str(mtime): | ||
| return datetime.datetime.fromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S') | ||
|
|
@@ -74,11 +73,8 @@ def http_server(sock, addr): | |
| size = os.path.getsize(filepath) | ||
| sock.sendall(build_header(guess_type(filepath)[0], os.path.getsize(filepath))) | ||
| with open(filepath, 'rb') as infile: | ||
| data = infile.read(CACHE_SIZE) | ||
| while data: | ||
| while data := infile.read(CACHE_SIZE): | ||
| sock.sendall(data) | ||
| data = infile.read(CACHE_SIZE) | ||
|
|
||
|
Comment on lines
-77
to
-81
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| elif os.path.isdir(filepath): | ||
| contents = os.listdir(filepath) | ||
| rep = html_str | ||
|
|
@@ -114,13 +110,9 @@ def http_server(sock, addr): | |
| rep = e.message | ||
| sock.sendall(build_header('text/plain', len(rep), e.code) + rep) | ||
|
|
||
| except: | ||
| rep = "500 Internal Server Error" | ||
| sock.sendall(build_header('text/plain', len(rep), '500') + rep) | ||
| pass | ||
| finally: | ||
| sock.shutdown(socket.SHUT_WR) | ||
| sock.close() | ||
| sock.shutdown(socket.SHUT_WR) | ||
| sock.close() | ||
|
|
||
|
|
||
| def receive_message(conn, buffsize=4096): | ||
|
|
@@ -143,22 +135,22 @@ def parse_request(request): | |
| first_rn = request.find('\r\n') | ||
| first_line = request[:first_rn] | ||
| if first_line.split()[0] == 'GET': | ||
| uri = first_line.split()[1] | ||
| return uri | ||
| return first_line.split()[1] | ||
|
Comment on lines
-146
to
+138
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| else: | ||
| raise Error405("405: Method not allowed. Only GET is allowed.") | ||
|
|
||
|
|
||
| def build_header(mimetype, bytelen, code="200 OK"): | ||
| """Build a response with the specified code and content.""" | ||
|
|
||
| resp_list = [] | ||
| resp_list.append('HTTP/1.1 %s' % code) | ||
| resp_list.append('Content-Type: %s; char=utf-8' % mimetype) | ||
| resp_list.append('Content-Length: %s' % str(bytelen)) | ||
| resp_list.append('\r\n') | ||
| resp = '\r\n'.join(resp_list) | ||
| return resp | ||
| resp_list = [ | ||
| f'HTTP/1.1 {code}', | ||
| f'Content-Type: {mimetype}; char=utf-8', | ||
| f'Content-Length: {str(bytelen)}', | ||
| '\r\n', | ||
| ] | ||
|
|
||
| return '\r\n'.join(resp_list) | ||
|
Comment on lines
-155
to
+153
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
size_strrefactored with the following changes:lift-return-into-if)