Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
sbws
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
juga
sbws
Commits
f55b84ac
Commit
f55b84ac
authored
6 years ago
by
Matt Traudt
Browse files
Options
Downloads
Patches
Plain Diff
Add simple HTTP server script
parent
57006a43
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
scripts/tools/sbws-http-server.py
+77
-0
77 additions, 0 deletions
scripts/tools/sbws-http-server.py
with
77 additions
and
0 deletions
scripts/tools/sbws-http-server.py
0 → 100755
+
77
−
0
View file @
f55b84ac
#!/usr/bin/env python3
# File: sbws-http-server.py
# Author: Matt Traudt
# License: CC0
#
# This script implements just enough of the HTTP protocol to work with Simple
# Bandwidth Scanner.
#
# All requested URLs exist. All return 1 GiB of garbage data. We always speak
# HTTP/1.1 because that's necessary for Keep-Alive request headers
# (used by sbws scanners) to work.
#
# HEAD and GET requests are supported to the minimum extent necessary.
# This essentially means that if the client sends Range request headers just
# like sbws does, then we'll only send back the number of bytes they requested.
# Indeed, this was the motivating reason for the complexity of this script;
# normally I would have used SimpleHTTPRequestHandler unmodified.
#
# Don't breathe too hard or this script might break.
from
argparse
import
ArgumentParser
,
ArgumentDefaultsHelpFormatter
import
http.server
from
http
import
HTTPStatus
# import time
FILE_SIZE
=
1
*
1024
*
1024
*
1024
# 1 GiB
def
_get_resp_size_from_range
(
range_str
):
assert
range_str
.
startswith
(
'
bytes=
'
)
range_str
=
range_str
[
len
(
'
bytes=
'
):]
start_byte
,
end_byte
=
range_str
.
split
(
'
-
'
)
return
int
(
end_byte
)
-
int
(
start_byte
)
+
1
class
MyHTTPRequestHandler
(
http
.
server
.
SimpleHTTPRequestHandler
):
protocol_version
=
'
HTTP/1.1
'
def
__init__
(
self
,
*
a
,
**
kw
):
super
().
__init__
(
*
a
,
**
kw
)
def
send_head
(
self
,
length
):
self
.
send_response
(
HTTPStatus
.
OK
)
self
.
send_header
(
'
Content-Type
'
,
'
application/octet-stream
'
)
self
.
send_header
(
'
Content-Length
'
,
length
)
# self.send_header('Last-Modified', self.date_time_string(time.time()))
self
.
end_headers
()
def
do_GET
(
self
):
range_hdr
=
self
.
headers
[
'
Range
'
]
if
not
range_hdr
:
num_bytes
=
FILE_SIZE
else
:
assert
range_hdr
.
startswith
(
'
bytes=
'
)
num_bytes
=
_get_resp_size_from_range
(
range_hdr
)
self
.
send_head
(
num_bytes
)
self
.
wfile
.
write
(
b
'
A
'
*
num_bytes
)
def
do_HEAD
(
self
):
self
.
send_head
(
FILE_SIZE
)
def
main
(
args
):
addr
=
(
''
,
args
.
port
)
print
(
'
Listening on
'
,
addr
)
httpd
=
http
.
server
.
HTTPServer
(
addr
,
MyHTTPRequestHandler
)
httpd
.
serve_forever
()
if
__name__
==
'
__main__
'
:
parser
=
ArgumentParser
(
formatter_class
=
ArgumentDefaultsHelpFormatter
)
parser
.
add_argument
(
'
-p
'
,
'
--port
'
,
default
=
8000
,
type
=
int
,
help
=
'
Port on which to listen
'
)
args
=
parser
.
parse_args
()
try
:
exit
(
main
(
args
))
except
KeyboardInterrupt
:
pass
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment