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

1.Networked Programs


1.Internet

我们现在学习Internet部分,即平时我们浏览器做的事情,之后再学习客服端这部分

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

2.TCP 传输控制协议

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

3.Socket

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

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

HTTP80端口用来与浏览器沟通

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

4.Sockets in Python

 mysock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#like file open
#AF_INET refer i'm make an internet socket
#STREAM refer i'm make an stream socket
mysock.connect(('www.py4inf.com',80))
#在我们这个程序和www.py4inf.com的80端口间建立一个Sockets
Python天然支持TCP Sockets
docs.python.org/library/socket.html  

2.From Sockets to Applications


1.HTTP 超文本传输协议

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

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

protocol        host                  document

2.Sockets

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

Click the Second Page is just a socket

3.Hacking HTTP

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

用telnet 加 GET去获取网页内容(Win7 默认不带telnet)

每次访问网页都是十几二十个GET,GET html、GET CSS、GET image....

3.Let's Write a Web Browser


1.An HTTP Request in Python

 import socket
mysock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#like file open
#AF_INET refer i'm make an internet socket
#STREAM refer i'm make an stream socket
mysock.connect(('www.py4inf.com',80))
#在我们这个程序和www.py4inf.com的80端口间建立一个Sockets
toSend='GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n'
mysock.send(toSend.encode('ascii'))
whileTrue:
data = mysock.recv(65)#65是buf长度,此处用来设置显示数据时的长度
if(len(data)<1):
break
print(data)
mysock.close()

2.编码错误,及其解决方法

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

使用encode 进行以下类型转换即可

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

3.Making HTTP Easier With urllib

socket比url更加接近底层,也就是说url更加简单。

socket是 Transport Layer , url是 Application Layer

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

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

4.Like a file

urllib turn URLs into files,所以我们可以像操作文件一样操作它
 import urllib.request
fhand=urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt')
counts=dict()
for line in fhand:
words=line.split()
for word in words:
counts[word]=counts.get(word,0)+1
print(counts)

Words:

subtlety 微妙

上一篇:Eclipse TestNg插件


下一篇:Java中ArrayList和LinkedList差别