some question about PartI-"downloading web page" #380
Answered
by
shuhei
zhang14725804
asked this question in
Q&A
-
def request(url):
# wrong code
scheme,url = url.split("://",1)
assert scheme in ["http","https"],"Unknown scheme {}".format(scheme)
port = 80 if scheme =="http" else 443
if scheme == "https":
ctx = ssl.create_default_context()
s = ctx.wrap_socket(s,server_hostname=host)
if ":" in host:
host,port = host.split(":",1)
port=int(port)
assert url.startswith("http://")
url = url[len("http://"):]
host,path = url.split("/",1)
path = "/"+path
s = socket.socket(
family=socket.AF_INET,
type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP,
)
s.connect(("example.org",80))
s.send(b"GET /index.html HTTP/1.0\r\n"+b"Host: example.org\r\n\r\n")
response = s.makefile("r",encoding="utf8",newline="\r\n")
statusline = response.readline()
version,status,explanation=statusline.split(" ",2)
assert status == "200","{}:{}".format(status,explanation)
headers = {}
while True:
line = response.readline()
if line =="\r\n":break
header,value = line.split(":",1)
headers[header.lower()]=value.strip()
body = response.read()
s.close()
return headers,body question1: my code is wrong about ssl part. What`s the right way to add ssl, and how to test ssl question2:Exercises is difficult for me, how and where to get the answer about Exercises |
Beta Was this translation helpful? Give feedback.
Answered by
shuhei
Mar 2, 2022
Replies: 1 comment 4 replies
-
For the question 1, a socket needs to be created before you wrap it with SSL (actually TLS). So, s = socket.socket(
family=socket.AF_INET,
type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP,
) should come before: if scheme == "https":
ctx = ssl.create_default_context()
s = ctx.wrap_socket(s,server_hostname=host) |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
pavpanchekha
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the question 1, a socket needs to be created before you wrap it with SSL (actually TLS). So,
should come before: