-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathexercise12_5.py
More file actions
executable file
·27 lines (24 loc) · 893 Bytes
/
exercise12_5.py
File metadata and controls
executable file
·27 lines (24 loc) · 893 Bytes
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
#!/usr/bin/env python3
"""
Exercise 12.5: (Advanced) Change the socket program so that it only shows
data after the headers and a blank line have been received. Remember that
recv is receiving characters (newlines and all), not lines.
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
"""
import socket
my_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
my_sock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode()
my_sock.send(cmd)
data = my_sock.recv(512)
message = data.decode()
header_end_pos = message.find('\r\n\r\n') + 4 # Finds the end of header
# Adds four to exclude:'\r\n\r\n'
print(message[header_end_pos:], end='')
while True: # Header in the first data only
data = my_sock.recv(512)
if not data:
break
print(data.decode())
my_sock.close()