Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cse490x 22sp Public
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
CSE 490X 22sp
Cse490x 22sp Public
Commits
e7c0b437
Commit
e7c0b437
authored
2 years ago
by
James R. Wilcox
Browse files
Options
Downloads
Patches
Plain Diff
post rest of chapter 8 code
parent
064fd848
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ch8/browser.py
+5
-3
5 additions, 3 deletions
ch8/browser.py
ch8/server.py
+50
-6
50 additions, 6 deletions
ch8/server.py
with
55 additions
and
9 deletions
ch8/browser.py
+
5
−
3
View file @
e7c0b437
...
...
@@ -142,7 +142,7 @@ class Tab:
url
=
elt
.
attributes
[
"
action
"
]
url
=
resolve_url
(
url
,
self
.
url
)
headers
,
body
=
request
(
url
,
body
)
self
.
load
(
url
,
body
)
def
scrolldown
(
self
):
max_y
=
max
(
self
.
document
.
height
-
(
HEIGHT
-
CHROME_PX
),
0
)
...
...
@@ -172,11 +172,12 @@ class Tab:
y
=
obj
.
y
-
self
.
scroll
+
CHROME_PX
canvas
.
create_line
(
x
,
y
,
x
,
y
+
obj
.
height
)
def
load
(
self
,
url
):
def
load
(
self
,
url
,
body
=
None
):
self
.
url
=
url
self
.
scroll
=
0
self
.
focus
=
None
self
.
history
.
append
(
url
)
headers
,
body
=
request
(
url
)
headers
,
body
=
request
(
url
,
body
)
self
.
nodes
=
HTMLParser
(
body
).
parse
()
print_tree
(
self
.
nodes
)
...
...
@@ -305,6 +306,7 @@ class Browser:
self
.
tabs
.
append
(
new_tab
)
self
.
draw
()
# if payload == None, then send a GET request
# otherwise, send a POST request with the payload as the body
def
request
(
url
,
payload
=
None
):
...
...
This diff is collapsed.
Click to expand it.
ch8/server.py
+
50
−
6
View file @
e7c0b437
import
socket
import
urllib.parse
counter
=
0
ENTRIES
=
[
"
James was here
"
]
def
show_comments
():
out
=
"
<!doctype html><html><body>
"
for
entry
in
ENTRIES
:
out
+=
f
"
<p>
{
entry
}
</p>
"
out
+=
"
<form action=add method=post>
"
out
+=
"
<p><input name=guest></p>
"
out
+=
"
<p><button>Sign the book!</button></p>
"
out
+=
"
</form>
"
out
+=
"
</body></html>
"
return
out
def
do_request
(
method
,
url
,
headers
,
body
):
print
(
method
,
url
,
headers
,
body
)
if
method
==
"
GET
"
and
url
==
"
/
"
:
return
"
200 OK
"
,
show_comments
()
elif
method
==
"
POST
"
and
url
==
"
/add
"
:
params
=
form_decode
(
body
)
return
"
200 OK
"
,
add_entry
(
params
)
else
:
return
"
404 Not Found
"
,
not_found
(
url
,
method
)
def
form_decode
(
body
):
params
=
{}
for
field
in
body
.
split
(
"
&
"
):
name
,
value
=
field
.
split
(
"
=
"
,
1
)
name
=
urllib
.
parse
.
unquote_plus
(
name
)
value
=
urllib
.
parse
.
unquote_plus
(
value
)
params
[
name
]
=
value
return
params
def
add_entry
(
params
):
if
'
guest
'
in
params
:
ENTRIES
.
append
(
params
[
'
guest
'
])
return
show_comments
()
def
not_found
(
url
,
method
):
out
=
"
<!doctype html>
"
out
+=
"
<h1>{} {} not found!</h1>
"
.
format
(
method
,
url
)
return
out
global
counter
counter
+=
1
return
"
200 OK
"
,
f
"
<html>hello you are the lucky
{
counter
}
th visitor</html>
"
def
handle_connection
(
conx
):
req
=
conx
.
makefile
(
"
b
"
)
...
...
@@ -20,7 +62,8 @@ def handle_connection(conx):
headers
=
{}
for
line
in
req
:
line
=
line
.
decode
(
'
utf8
'
)
if
line
==
'
\r\n
'
:
break
if
line
==
'
\r\n
'
:
break
header
,
value
=
line
.
split
(
"
:
"
,
1
)
headers
[
header
.
lower
()]
=
value
.
strip
()
...
...
@@ -40,6 +83,7 @@ def handle_connection(conx):
conx
.
send
(
response
.
encode
(
'
utf8
'
))
conx
.
close
()
if
__name__
==
"
__main__
"
:
s
=
socket
.
socket
(
family
=
socket
.
AF_INET
,
...
...
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