您的位置:首页 > 理论基础 > 计算机网络

Python 网络编程基础入门

2019-01-10 15:02 483 查看

Python 网络编程基础入门

Python的网络编程主要支持两种网络协议:TCP和UDP。这两种协议都通过叫Socket的编程抽象进行处理。Socket起源于Unix,是类似于文件的存在,可以像文件一样进行I/O、打开、关闭等操作,最主要的是它可以实现网络上不同主机的进程间通信,所以基本上Socket是任何一种网络通讯中最基础的内容。

进群进群:700341555可以获取Python各类入门学习资料!

这是我的微信公众号【Python编程之家】各位大佬用空可以关注下,每天更新Python学习方法,感谢!

111111111111.png

 

Python中建立一个套接字很简单:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. import socket

  2. s = socket.socket(family, type)

</pre>

地址族

family为地址族,该族指定要使用的网络协议,主要使用的有:

  • AF_INET:IPv4协议(TCP,UDP)

  • AF_INET6:IPv6协议(TCP,UDP)

  • AF_UNIX:UNIX域协议,用于同一台机器的进程间通讯

套接字类型

type为套接字类型,指定给定的协议组中使用的通信类型:

  • SOCK_STREAM:用于TCP

  • SOCK_DGRAM:用于UDP

TCP和UDP都是基于Client/Server的编程模型,所以Socket编程也分为客户端和服务器端,以TCP为例:

TCP客户端编程

要获取远程主机的ip地址,可以使用socket标准库提供的gethostbyname方法:

socket套接字实例s可用于客户端的方法有以下几个:

  • s.connect(addr):连接服务器端套接字。addr格式取决于地址族,对于IPv4来说,是一个包含ip地址与端口的元组,(host, port)。连接失败会报socket.error错误。

  • s.sendall(string):尝试发送所有数据,成功则返回None,失败则报异常。

  • s.recv(bufsize):接收数据,bufsize指定接收的最大数据量。

  • s.close:关闭套接字

OK,现在可以用socket向远程主机发送一个HTTP GET请求了:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. # -*- coding: utf-8 -*-

  2. import socket

  3. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #建立套接字

  4. host = 'www.baidu.com'

  5. port = 80

  6. ip = socket.gethostbyname(host) #获取ip

  7. s.connect((ip, port)) #建立连接

  8. message = 'GET / HTTP/1.1rnrn'

  9. s.sendall(message) #发送GET请求

  10. r = s.recv(4096) #接收数据

  11. print r

  12. s.close #关闭套接字

</pre>

返回:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. HTTP/1.1 302 Moved Temporarily

  2. Date: Wed, 10 Jan 2018 18:56:45 GMT

  3. Content-Type: text/html

  4. Content-Length: 225

  5. Connection: Keep-Alive

  6. Location: http://www.baidu.com/search/error.html

  7. Server: BWS/1.1

  8. X-UA-Compatible: IE=Edge,chrome=1

  9. BDPAGETYPE: 3

  10. Set-Cookie: BDSVRTM=0; path=/

</pre>

下面我们可以实现自己的服务器。

TCP服务器端编程

Socket实例与服务器端编程有关的方法有以下几个:

  • s.bind(addr):addr也是(host, port)形式的元组,将套接字绑定到特定的地址和端口上。空字符串表示任意地址,'broadcast'可以用做发送广播信息。

  • s.listen(backlog):开始监听连接,backlog为最大挂起连接次数。

  • s.accept:返回元组(conn,addr),conn为新的套接字,可以用来发送和接收数据。addr是客户端的套接字地址。

  • s.recv、s.sendall和s.close与客户端同。

现在写一个将客户端发送来的信息发送回去的服务器:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. # -*- coding: utf-8 -*-

  2. import socket

  3. import sys

  4. HOST = ''

  5. PORT = 8088

  6. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  7. s.bind((HOST, PORT))

  8. s.listen(5)

  9. print '开始监听'

  10. conn, addr = s.accept

  11. print 'Connected with ' + addr[0] + ':' + str(addr[1])

  12. data = conn.recv(1024)

  13. conn.sendall(data)

  14. conn.close

  15. s.close

</pre>

运行:

服务器开始监听连接了。修改一下刚才写的客户端程序:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. # -*- coding: utf-8 -*-

  2. import socket

  3. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  4. host = 'localhost'

  5. port = 8088

  6. s.connect((host, port)) #建立连接

  7. message = 'GET / HTTP/1.1rnrn'

  8. s.sendall(message) #发送GET请求

  9. r = s.recv(4096) #接收数据

  10. print r

  11. s.close #关闭套接字

</pre>

运行,连接本地的服务器,服务器端输出:

连接成功。客户端输出:

发送的消息被返回了。

这就是一个最简单的服务器了。上述服务器只能处理一次连接,这显然不是我们想看到的,保持一直运行:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. # -*- coding: utf-8 -*-

  2. import socket

  3. import sys

  4. HOST = ''

  5. PORT = 8088

  6. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  7. s.bind((HOST, PORT))

  8. s.listen(5)

  9. print '开始监听'

  10. while True:

  11. conn, addr = s.accept

  12. print 'Connected with ' + addr[0] + ':' + str(addr[1])

  13. data = conn.recv(1024)

  14. conn.sendall(data)

  15. conn.close

  16. s.close

</pre>

现在就可以使用客户端无限连接了:

服务器端多线程处理连接

现在服务器端虽然可以处理无限多个连接,但只能一个一个的处理,后面的客户端连接只能等待前面的连接完成才能发送数据。要同时处理多个连接,可以使用多线程。服务器端接收到新的连接后,开启一个线程处理新连接,主线程去建立下一个连接。

服务器端:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. # -*- coding: utf-8 -*-

  2. import socket

  3. import threading

  4. HOST = ''

  5. PORT = 8088

  6. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  7. s.bind((HOST, PORT))

  8. s.listen(5)

  9. print '开始监听'

  10. def runThread(conn):

  11. data = conn.recv(1024)

  12. print data

  13. conn.sendall(data)

  14. conn.close

  15. while True:

  16. conn, addr = s.accept

  17. print 'Connected with ' + addr[0] + ':' + str(addr[1])

  18. t = threading.Thread(target=runThread, args=(conn,))

  19. t.daemon = True

  20. t.start

</pre>

客户端启动多个连接:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. # -*- coding: utf-8 -*-

  2. import socket

  3. import time

  4. import threading

  5. def run:

  6. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  7. host = 'localhost'

  8. port = 8088

  9. s.connect((host, port))

  10. message = 'GET / HTTP/1.1rnrn'

  11. s.sendall(message)

  12. print s.recv(4096)

  13. s.close

  14. if __name__ == '__main__':

  15. for i in xrange(4):

  16. t = threading.Thread(target=run)

  17. t.start

</pre>

运行:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. 开始监听

  2. Connected with 127.0.0.1:61772

  3. GET / HTTP/1.1

  4. Connected with 127.0.0.1:61773

  5. GET / HTTP/1.1

  6. Connected with 127.0.0.1:61774

  7. GET / HTTP/1.1

  8. Connected with 127.0.0.1:61775

  9. GET / HTTP/1.1

</pre>

UDP编程

UDP与TCP的不同之处在于UDP是不用建立连接的。

在此需要使用s.recvfrom与s.sendto方法,前者与s.recv相同,但返回(data, addr)的元组,addr为数据发送端的套接字地址,后者发送数据时需要加入要发送的远程地址。

服务器:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. # -*- coding: utf-8 -*-

  2. import socket

  3. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

  4. s.bind(('', 10000))

  5. while True:

  6. data, addr = s.recvfrom(1024)

  7. print '接收到%s的连接'%str(addr)

  8. s.sendto(data, addr)

</pre>

客户端:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. # -*- coding: utf-8 -*-

  2. import socket

  3. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

  4. s.sendto('Hello World', ('localhost', 10000))

  5. r, addr = s.recvfrom(1024)

  6. print r

  7. s.close

</pre>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: