These are my notes on networked programs


In [43]:
# Python built in support for TCP sockets
import socket

# this just opens a 'porthole' out from my computer
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# this connects me to the other computer
mysock.connect(('www.py4inf.com', 80))

Now let's write a browser

(the request response cycle)

  1. Make a connection (a socket)
  2. Send a request
  3. Receive a response
  4. Display that response

In [63]:
import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send(b'GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')

while True:
    data = mysock.recv(512)
    if ( len(data) < 1 ) :
        break
    print(data);

mysock.close()


b'HTTP/1.1 404 Not Found\r\nServer: nginx\r\nDate: Thu, 11 May 2017 19:18:52 GMT\r\nContent-Type: text/html\r\nContent-Length: 162\r\nConnection: close\r\n\r\n<html>\r\n<head><title>404 Not Found</title></head>\r\n<body bgcolor="white">\r\n<center><h1>404 Not Found</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n'

In [55]:
import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))

# since I need to send bytes and not a string... I add a 'b' literal before the GET
mysock.send(b'GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')

while True:
    data = mysock.recv(512)
    if ( len(data) < 1 ) :
        break
    print(data);

mysock.close()


b'HTTP/1.1 404 Not Found\r\nServer: nginx\r\nDate: Thu, 11 May 2017 18:36:42 GMT\r\nContent-Type: text/html\r\nContent-Length: 162\r\nConnection: close\r\n\r\n<html>\r\n<head><title>404 Not Found</title></head>\r\n<body bgcolor="white">\r\n<center><h1>404 Not Found</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n'

Now make the same thing easier with another library

urllib - knows about the GET, the headers, the blank lines, port 80 and all the rest This is similar to the code in the course but for Python 3


In [29]:
import urllib.request

fhand = urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt')

# the response needs to be translated to html using read()
fhand_html = fhand.read()

print(fhand_html)


fhand_html

In [38]:
# a nicer version of the code...

import urllib.request

with urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt') as response:
    fhand_html = response.read()
    #fhand_html = response.readline()
    #fhand_html = response.readlines()
    
print(fhand_html)
    
# read() - will store the response as a string
# readline() - will store only the first line as a string
# readlines() - will store the response as a list


b'But soft what light through yonder window breaks\nIt is the east and Juliet is the sun\nArise fair sun and kill the envious moon\nWho is already sick and pale with grief\n'

Doing the assignment:


In [70]:
# https://pymotw.com/3/urllib.request/

from urllib import request
URL = 'http://data.pr4e.org/intro-short.txt'

response = request.urlopen(URL)
print('RESPONSE:', response)
print('URL     :', response.geturl())

headers = response.info()
print('DATE    :', headers['date'])
print('HEADERS :')
print('---------')
print(headers)

data = response.read().decode('utf-8')
print('LENGTH  :', len(data))
print('DATA    :')
print('---------')
print(data)


RESPONSE: <http.client.HTTPResponse object at 0x7f56a36f4978>
URL     : http://data.pr4e.org/intro-short.txt
DATE    : Thu, 11 May 2017 19:24:25 GMT
HEADERS :
---------
Content-Type: text/plain
Content-Length: 467
Connection: close
Date: Thu, 11 May 2017 19:24:25 GMT
Server: Apache
Last-Modified: Sat, 24 Sep 2016 20:36:08 GMT
ETag: "1d3-53d46d841582a"
Accept-Ranges: bytes


LENGTH  : 467
DATA    :
---------
Why should you learn to write programs?

Writing programs (or programming) is a very creative 
and rewarding activity.  You can write programs for 
many reasons, ranging from making your living to solving
a difficult data analysis problem to having fun to helping
someone else solve a problem.  This book assumes that 
everyone needs to know how to program, and that once 
you know how to program you will figure out what you want 
to do with your newfound skills.  


In [68]:
# getting the response code, with error handling

import urllib.request
URL = 'http://data.pr4e.org/intro-short.txt'

try:
    response = urllib.request.urlopen(URL)
    if response.getcode() == 200:
        print('Bingo')
    else:
        print('The response code was not 200, but: {}'.format(
            response.get_code()))
except urllib.error.HTTPError as e:
    print('''An error occurred: {}
The response code was {}'''.format(e, e.getcode()))


Bingo

In [ ]: