-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExploring the HyperText Transport Protocol.py
More file actions
47 lines (44 loc) · 2.2 KB
/
Exploring the HyperText Transport Protocol.py
File metadata and controls
47 lines (44 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Welcome ADEWOYE AYODEJI ADESHOLA from Using Python to Access Web Data
#
# Exploring the HyperText Transport Protocol
#
# You are to retrieve the following document using the HTTP protocol
# in a way that you can examine the HTTP Response headers.
#
# http://data.pr4e.org/intro-short.txt
#
# There are three ways that you might retrieve this web page
# and look at the response headers:
#
# Preferred: Modify the socket1.py program to retrieve the above URL
# and print out the headers and data.
# Make sure to change the code to retrieve the above URL -
# the values are different for each URL.
# Open the URL in a web browser with a developer console
# or FireBug and manually examine the headers that are returned.
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# This line doesn't actually make the connection.
# This is more like a door way.
# Think of this line like a file handle,
# that doesn't have any data associated with it yet.
#
mysock.connect(('data.pr4e.org', 80))
#This line connects our socket
# to a destination across the internet
# with a domain name "data,pr4e.org", using port 80
#
cmd = 'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\n\n'.encode()
# The GET all the way to HTTP/1.0 is the http rules
# The first \n is 'end of line'
# The second \n is 'blank line'
# The ".encode()" is there because there are strings inside python
# that are in unicode that needs to be sent out in UTF-8 bytes
#
mysock.send(cmd) # This line send the command 'cmd'
while True:
data = mysock.recv(152)
if len(data) < 1: # This line denotes end of file since that is when its length will be less than 1
break
print(data.decode())
mysock.close() # This closes the socket