<jupyter_start><jupyter_text>Capítulo 12 - Programas em rede  12.1 - HyperText Transfer Protocol - HTTP  12.2 - O navegador da internet mais simples do mundo<jupyter_code>import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
    print(data.decode(),end='')

mysock.close()

# Code: http://www.py4e.com/code3/socket1.py<jupyter_output>HTTP/1.1 200 OK
Date: Fri, 25 Feb 2022 13:12:43 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Sat, 13 May 2017 11:22:22 GMT
ETag: "a7-54f6609245537"
Accept-Ranges: bytes
Content-Length: 167
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Connection: close
Content-Type: text/plain

But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief<jupyter_text>os métodos **encode()** e **decode()** convertem strings em objetos em bytes e vice versa.<jupyter_code>print(b'Hello world')
print('Hello world'.encode())<jupyter_output>b'Hello world'<jupyter_text>a notação b' ' especofoca a variável que precisa ser armazenada como um objeto em bytes, encode() e b' ' são equivalentes  12.3 - Recuperando uma imagen sovre http<jupyter_code>import socket
import time

HOST = 'data.pr4e.org'
PORT = 80
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((HOST, PORT))
mysock.sendall(b'GET http://data.pr4e.org/cover3.jpg HTTP/1.0\r\n\r\n')
count = 0
picture = b""

while True:
    data = mysock.recv(5120)
    if len(data) < 1:
        break
    #time.sleep(0.25)
    count = count + len(data)
    print(len(data), count)
    picture = picture + data

mysock.close()

# Look for the end of the header (2 CRLF)
pos = picture.find(b"\r\n\r\n")
print('Header length', pos)
print(picture[:pos].decode())

# Skip past the header and save the picture data
picture = picture[pos+4:]
fhand = open("stuff.jpg", "wb")
fhand.write(picture)
fhand.close()<jupyter_output>1452 1452
5120 6572
5120 11692
2828 14520
4356 18876
5120 23996
2140 26136
1452 27588
5120 32708
5120 37828
2828 40656
2904 43560
1452 45012
1452 46464
5120 51584
5120 56704
1376 58080
1452 59532
5120 64652
5120 69772
5120 74892
5120 80012
5120 85132
5120 90252
5120 95372
460 95832
4356 100188
1452 101640
1452 103092
5120 108212
688 108900
1452 110352
5120 115472
5120 120592
5120 125712
4968 130680
1452 132132
5120 137252
5120 142372
5120 147492
5120 152612
5120 157732
5120 162852
5120 167972
4816 172788
5120 177908
688 178596
1452 180048
4356 184404
5120 189524
688 190212
5120 195332
3592 198924
1452 200376
1452 201828
5120 206948
5120 212068
5120 217188
5120 222308
5120 227428
3180 230608
Header length 394
HTTP/1.1 200 OK
Date: Fri, 25 Feb 2022 13:24:28 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Mon, 15 May 2017 12:27:40 GMT
ETag: "38342-54f8f2e5b6277"
Accept-Ranges: bytes
Content-Length: 230210
Vary: Accept-Encoding
Cache-Control: max-age=0, no-cache, no-store, must-revalidat[...]
{'fraction_non_alphanumeric': 0.10363015823766678, 'fraction_numerical': 0.2292894818492088, 'mean_word_length': 4.391304347826087, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '356497', 'license': 'MIT', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1261, 'n_words': 385, 'path': 'book_pt_br/capitulo_12/.ipynb_checkpoints/capitulo12-checkpoint.ipynb', 'repo_name': 'tfn10/python-for-everybody', 'size': '3223'}