Python Web-第三週-Networks and Sockets(Using Python to Access Web Data)

1.Networked Programs


1.Internet

咱們如今學習Internet部分,即平時咱們瀏覽器作的事情,以後再學習客服端這部分


2.TCP 傳輸控制協議


3.Socket


 


HTTP80端口用來與瀏覽器溝通html


4.Sockets in Python

1 mysock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#like file open
2 #AF_INET refer i'm make an internet socket
3 #STREAM refer i'm make an stream socket
4 mysock.connect(('www.py4inf.com',80))
5 #在咱們這個程序和www.py4inf.com的80端口間創建一個Sockets
Python自然支持TCP Sockets
docs.python.org/library/socket.html  

2.From Sockets to Applications


1.HTTP 超文本傳輸協議


http://www.dr-chuck.com/page1.htm
python

protocol        host                  document瀏覽器

2.Sockets


Click the Second Page is just a socketsocket

3.Hacking HTTP


用telnet 加 GET去獲取網頁內容(Win7 默認不帶telnet)學習

每次訪問網頁都是十幾二十個GET,GET html、GET CSS、GET image....編碼

3.Let's Write a Web Browser


1.An HTTP Request in Python

 1 import socket
 2 mysock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#like file open
 3 #AF_INET refer i'm make an internet socket
 4 #STREAM refer i'm make an stream socket
 5 mysock.connect(('www.py4inf.com',80))
 6 #在咱們這個程序和www.py4inf.com的80端口間創建一個Sockets
 7 toSend='GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n'
 8 mysock.send(toSend.encode('ascii'))
 9 whileTrue:
10 data = mysock.recv(65)#65是buf長度,此處用來設置顯示數據時的長度
11 if(len(data)<1):
12 break
13 print(data)
14 mysock.close()

2.編碼錯誤,及其解決方法

使用encode 進行如下類型轉換便可url

1 toSend='GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n'
2 mysock.send(toSend.encode('ascii'))

3.Making HTTP Easier With urllib

socket比url更加接近底層,也就是說url更加簡單。spa

socket是 Transport Layer , url是 Application Layercode

 

注:2.x版本python使用import urllib,但3.x版本python使用的是import urllib.requestorm

1 import urllib.request
2 fhand=urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt')
3 for line in fhand:
4 print(line.strip())

4.Like a file

urllib turn URLs  into files,因此咱們能夠像操做文件同樣操做它
1 import urllib.request
2 fhand=urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt')
3 counts=dict()
4 for line in fhand:
5 words=line.split()
6 for word in words:
7 counts[word]=counts.get(word,0)+1
8 print(counts)

Words:

subtlety 微妙

相關文章
相關標籤/搜索