Socket Programming

socket_programmingSocket Programming HOWTO
https://docs.python.org/3.5/howto/sockets.html

18.1. socket — Low-level networking interface
https://docs.python.org/3/library/socket.html

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((‘www.py4inf.com’, 80))
s.send(b‘GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n’)
while True:
____data = s.recv(512)    #or 1024
____if (len(data) < 1):
________break
____print(data.decode(‘UTF-8’))
s.close()

==================================

21.6. urllib.request — Extensible library for opening URLs
https://docs.python.org/3.5/library/urllib.request.html

import urllib.request

url = ‘http://www.py4inf.com/code/romeo.txt
s = urllib.request.urlopen(url)
for line in s:
____print(line.decode(‘UTF-8’).strip())

————————————————————-

import urllib.request

url = ‘http://www.py4inf.com/code/romeo.txt

with urllib.request.urlopen(url) as f:
____print(f.read().decode(‘utf-8’))

from:
https://docs.python.org/3/library/urllib.request.html#examples

The with statement
https://docs.python.org/3.4/reference/compound_stmts.html#the-with-statement

—————————————————————

import urllib.request
url = ‘http://www.py4inf.com/code/romeo.txt
local_filename, headers = urllib.request.urlretrieve(url)
print(open(local_filename).read())

get_content_typePython: How to get the Content-Type of an URL?

from:
Using Python to Access Web Data
Coursera, Oct 26 — Dec 14, 2015.
https://www.coursera.org/learn/python-network-data/lecture/UxIOc/lets-write-a-web-browser

for bytes() or decode():
http://stackoverflow.com/questions/5471158/typeerror-str-does-not-support-the-buffer-interface

for more info on the “with” statement:
help(‘with’)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s