Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ap-12_http_server
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Давид Ли
ap-12_http_server
Commits
a16ad841
Commit
a16ad841
authored
Nov 27, 2023
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 35
parents
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
0 deletions
+82
-0
.gitignore
.gitignore
+2
-0
http_server.py
http_server.py
+37
-0
request.py
request.py
+43
-0
No files found.
.gitignore
0 → 100644
View file @
a16ad841
__pycache__/*
\ No newline at end of file
http_server.py
0 → 100644
View file @
a16ad841
from
request
import
Request
import
socketserver
class
Handler
(
socketserver
.
StreamRequestHandler
):
# self.request - client socket
def
handle
(
self
):
request
=
Request
(
self
.
rfile
)
print
(
request
)
response_body
=
'<h1>Hello World!</h1>'
response_body_len
=
len
(
response_body
.
encode
())
response
=
(
'HTTP/1.1 200 OK'
,
'Content-Type: text/html'
,
f
'Content-Length: {response_body_len}'
,
'Connection: close'
,
''
,
response_body
,
)
self
.
wfile
.
write
(
'
\r\n
'
.
join
(
response
)
.
encode
())
# CR - \r
# LF - \n
# b'GET / HTTP/1.1\r\nHost: localhost:1026\r\nConnection: keep-alive\r\nsec-ch-ua: "Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"\r\nsec-ch-ua-mobile: ?0\r\nsec-ch-ua-platform: "macOS"\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\nSec-Fetch-Site: none\r\nSec-Fetch-Mode: navigate\r\nSec-Fetch-User: ?1\r\nSec-Fetch-Dest: document\r\nAccept-Encoding: gzip, deflate, br\r\nAccept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7\r\n
ADDRESS
=
(
'localhost'
,
1026
)
socketserver
.
ThreadingTCPServer
.
allow_reuse_address
=
True
with
socketserver
.
ThreadingTCPServer
(
ADDRESS
,
Handler
)
as
server
:
server
.
serve_forever
()
request.py
0 → 100644
View file @
a16ad841
class
Request
:
def
__init__
(
self
,
rfile
):
self
.
file
=
rfile
self
.
method
=
None
self
.
uri
=
None
self
.
protocol
=
None
self
.
headers
=
{}
self
.
body
=
None
self
.
__parse_request_line
()
self
.
__parse_headers
()
self
.
__parse_body
()
def
__parse_request_line
(
self
):
request_line
=
self
.
__readline
()
# GET / HTTP/1.1 ['GET', '/', 'HTTP/1.1']
self
.
method
,
self
.
uri
,
self
.
protocol
=
request_line
.
split
()
def
__readline
(
self
):
return
self
.
file
.
readline
()
.
decode
()
.
strip
()
def
__parse_headers
(
self
):
while
True
:
header
=
self
.
__readline
()
if
header
==
''
:
break
name
,
value
=
header
.
split
(
': '
)
self
.
headers
[
name
]
=
value
def
__parse_body
(
self
):
if
'Content-Length'
in
self
.
headers
:
content_length
=
int
(
self
.
headers
[
'Content-Length'
])
self
.
body
=
self
.
file
.
read
(
content_length
)
def
__str__
(
self
):
return
(
f
'{"-" * 30}
\n
'
f
'{self.method} {self.uri}'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment