Python 的 Socket 编程教程

这是用来快速学习 Python Socket 套接字编程的指南和教程。Python 的 Socket 编程跟 C 语言很像。

Python 官方关于 Socket 的函数请看 http://docs.python.org/library/socket.html

基本上,Socket 是任何一种计算机网络通讯中最基础的内容。例如当你在浏览器地址栏中输入 www.oschina.net 时,你会打开一个套接字,然后连接到 www.oschina.net 并读取响应的页面然后然后显示出来。而其他一些聊天客户端如 gtalk 和 skype 也是类似。任何网络通讯都是通过 Socket 来完成的。

写在开头

本教程假设你已经有一些基本的 Python 编程的知识。

让我们开始 Socket 编程吧。

创建 Socket

首先要做的就是创建一个 Socket,socket 的 socket 函数可以实现,代码如下:

?

Code

1

2

3

4

5

6

7

8

#Socket client example in python

import socket   #for sockets

#create an AF_INET, STREAM socket (TCP)

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

print ‘Socket Created‘

函数 socket.socket 创建了一个 Socket,并返回 Socket 的描述符可用于其他 Socket 相关的函数。

上述代码使用了下面两个属性来创建 Socket:

地址簇 : AF_INET (IPv4)
类型: SOCK_STREAM (使用 TCP 传输控制协议)

错误处理

如果 socket 函数失败了,python 将抛出一个名为 socket.error 的异常,这个异常必须予以处理:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

#handling errors in python socket programs

import socket   #for sockets

import sys  #for exit

try:

    #create an AF_INET, STREAM socket (TCP)

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

except socket.error, msg:

    print ‘Failed to create socket. Error code: ‘ + str(msg[0]) + ‘ , Error message : ‘ + msg[1]

    sys.exit();

print ‘Socket Created‘

好了,假设你已经成功创建了 Socket,下一步该做什么呢?接下来我们将使用这个 Socket 来连接到服务器。

注意

与 SOCK_STREAM 相对应的其他类型是 SOCK_DGRAM 用于 UDP 通讯协议,UDP 通讯是非连接 Socket,在这篇文章中我们只讨论 SOCK_STREAM ,或者叫 TCP 。

连接到服务器

连接到服务器需要服务器地址和端口号,这里使用的是 www.oschina.net 和 80 端口。

首先获取远程主机的 IP 地址

连接到远程主机之前,我们需要知道它的 IP 地址,在 Python 中,获取 IP 地址是很简单的:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

import socket   #for sockets

import sys  #for exit

try:

    #create an AF_INET, STREAM socket (TCP)

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

except socket.error, msg:

    print ‘Failed to create socket. Error code: ‘ + str(msg[0]) + ‘ , Error message : ‘ + msg[1]

    sys.exit();

print ‘Socket Created‘

host = ‘www.oschina.net‘

try:

    remote_ip = socket.gethostbyname( host )

except socket.gaierror:

    #could not resolve

    print ‘Hostname could not be resolved. Exiting‘

    sys.exit()

    

print ‘Ip address of ‘ + host + ‘ is ‘ + remote_ip

我们已经有 IP 地址了,接下来需要指定要连接的端口。

代码:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

import socket   #for sockets

import sys  #for exit

try:

    #create an AF_INET, STREAM socket (TCP)

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

except socket.error, msg:

    print ‘Failed to create socket. Error code: ‘ + str(msg[0]) + ‘ , Error message : ‘ + msg[1]

    sys.exit();

print ‘Socket Created‘

host = ‘www.oschina.net‘

port = 80

try:

    remote_ip = socket.gethostbyname( host )

except socket.gaierror:

    #could not resolve

    print ‘Hostname could not be resolved. Exiting‘

    sys.exit()

    

print ‘Ip address of ‘ + host + ‘ is ‘ + remote_ip

#Connect to remote server

s.connect((remote_ip , port))

print ‘Socket Connected to ‘ + host + ‘ on ip ‘ + remote_ip

现在运行程序

?


1

2

3

4

$ python client.py

Socket Created

Ip address of www.oschina.net is 61.145.122.155

Socket Connected to www.oschina.net on ip 61.145.122.155

这段程序创建了一个 Socket 并进行连接,试试使用其他一些不存在的端口(如81)会是怎样?这个逻辑相当于构建了一个端口扫描器。

已经连接上了,接下来就是往服务器上发送数据。

免费提示

使用 SOCK_STREAM/TCP 套接字才有“连接”的概念。连接意味着可靠的数据流通讯机制,可以同时有多个数据流。可以想象成一个数据互不干扰的管道。另外一个重要的提示是:数据包的发送和接收是有顺序的。

其他一些 Socket 如 UDP、ICMP 和 ARP 没有“连接”的概念,它们是无连接通讯,意味着你可从任何人或者给任何人发送和接收数据包。

发送数据

sendall 函数用于简单的发送数据,我们来向 oschina 发送一些数据:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

import socket   #for sockets

import sys  #for exit

try:

    #create an AF_INET, STREAM socket (TCP)

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

except socket.error, msg:

    print ‘Failed to create socket. Error code: ‘ + str(msg[0]) + ‘ , Error message : ‘ + msg[1]

    sys.exit();

print ‘Socket Created‘

host = ‘www.oschina.net‘

port = 80

try:

    remote_ip = socket.gethostbyname( host )

except socket.gaierror:

    #could not resolve

    print ‘Hostname could not be resolved. Exiting‘

    sys.exit()

    

print ‘Ip address of ‘ + host + ‘ is ‘ + remote_ip

#Connect to remote server

s.connect((remote_ip , port))

print ‘Socket Connected to ‘ + host + ‘ on ip ‘ + remote_ip

#Send some data to remote server

message = "GET / HTTP/1.1\r\n\r\n"

try :

    #Set the whole string

    s.sendall(message)

except socket.error:

    #Send failed

    print ‘Send failed‘

    sys.exit()

print ‘Message send successfully‘

上述例子中,首先连接到目标服务器,然后发送字符串数据 "GET / HTTP/1.1\r\n\r\n" ,这是一个 HTTP 协议的命令,用来获取网站首页的内容。

接下来需要读取服务器返回的数据。

接收数据

recv 函数用于从 socket 接收数据:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

#Socket client example in python

import socket   #for sockets

import sys  #for exit

#create an INET, STREAMing socket

try:

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

except socket.error:

    print ‘Failed to create socket‘

    sys.exit()

    

print ‘Socket Created‘

host = ‘oschina.net‘;

port = 80;

try:

    remote_ip = socket.gethostbyname( host )

except socket.gaierror:

    #could not resolve

    print ‘Hostname could not be resolved. Exiting‘

    sys.exit()

#Connect to remote server

s.connect((remote_ip , port))

print ‘Socket Connected to ‘ + host + ‘ on ip ‘ + remote_ip

#Send some data to remote server

message = "GET / HTTP/1.1\r\nHost: oschina.net\r\n\r\n"

try :

    #Set the whole string

    s.sendall(message)

except socket.error:

    #Send failed

    print ‘Send failed‘

    sys.exit()

print ‘Message send successfully‘

#Now receive data

reply = s.recv(4096)

print reply

下面是上述程序执行的结果:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

$ python client.py

Socket Created

Ip address of oschina.net is 61.145.122.

Socket Connected to oschina.net on ip 61.145.122.155

Message send successfully

HTTP/1.1 301 Moved Permanently

Server: nginx

Date: Wed, 24 Oct 2012 13:26:46 GMT

Content-Type: text/html

Content-Length: 178

Connection: keep-alive

Keep-Alive: timeout=20

Location: http://www.oschina.net/

oschina.net 回应了我们所请求的 URL 的内容,很简单。数据接收完了,可以关闭 Socket 了。

关闭 socket

close 函数用于关闭 Socket:

?

Code

1

s.close()

这就是了。

让我们回顾一下

上述的示例中我们学到了如何:

1. 创建 Socket
2. 连接到远程服务器
3. 发送数据
4. 接收回应

当你用浏览器打开 www.oschina.net
时,其过程也是一样。包含两种类型,分别是客户端和服务器,客户端连接到服务器并读取数据,服务器使用 Socket
接收进入的连接并提供数据。因此在这里 www.oschina.net 是服务器端,而你的浏览器是客户端。

接下来我们开始在服务器端做点编码。

服务器端编程

服务器端编程主要包括下面几步:

1. 打开 socket
2. 绑定到一个地址和端口
3. 侦听进来的连接
4. 接受连接
5. 读写数据

我们已经学习过如何打开 Socket 了,下面是绑定到指定的地址和端口上。

绑定 Socket

bind 函数用于将 Socket 绑定到一个特定的地址和端口,它需要一个类似 connect 函数所需的 sockaddr_in 结构体。

示例代码:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

import socket

import sys

HOST = ‘‘   # Symbolic name meaning all available interfaces

PORT = 8888 # Arbitrary non-privileged port

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

print ‘Socket created‘

try:

    s.bind((HOST, PORT))

except socket.error , msg:

    print ‘Bind failed. Error Code : ‘ + str(msg[0]) + ‘ Message ‘ + msg[1]

    sys.exit()

    

print ‘Socket bind complete‘

绑定完成后,就需要让 Socket 开始侦听连接。很显然,你不能将两个不同的 Socket 绑定到同一个端口之上。

连接侦听

绑定 Socket 之后就可以开始侦听连接,我们需要将 Socket 变成侦听模式。socket 的 listen 函数用于实现侦听模式:

?

Code

1

2

s.listen(10)

print ‘Socket now listening‘

listen 函数所需的参数成为 backlog,用来控制程序忙时可保持等待状态的连接数。这里我们传递的是 10,意味着如果已经有 10 个连接在等待处理,那么第 11 个连接将会被拒绝。当检查了 socket_accept 后这个会更加清晰。

接受连接

示例代码:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

import socket

import sys

HOST = ‘‘   # Symbolic name meaning all available interfaces

PORT = 8888 # Arbitrary non-privileged port

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

print ‘Socket created‘

try:

    s.bind((HOST, PORT))

except socket.error , msg:

    print ‘Bind failed. Error Code : ‘ + str(msg[0]) + ‘ Message ‘ + msg[1]

    sys.exit()

    

print ‘Socket bind complete‘

s.listen(10)

print ‘Socket now listening‘

#wait to accept a connection - blocking call

conn, addr = s.accept()

#display client information

print ‘Connected with ‘ + addr[0] + ‘:‘ + str(addr[1])

输出

运行该程序将会显示:

?


1

2

3

4

$ python server.py

Socket created

Socket bind complete

Socket now listening

现在这个程序开始等待连接进入,端口是 8888,请不要关闭这个程序,我们来通过 telnet 程序来进行测试。

打开命令行窗口并输入:

?


1

2

3

4

5

6

7

8

$ telnet localhost 8888

It will immediately show

$ telnet localhost 8888

Trying 127.0.0.1...

Connected to localhost.

Escape character is ‘^]‘.

Connection closed by foreign host.

而服务器端窗口显示的是:

?


1

2

3

4

5

$ python server.py

Socket created

Socket bind complete

Socket now listening

Connected with 127.0.0.1:59954

我们可看到客户端已经成功连接到服务器。

上面例子我们接收到连接并立即关闭,这样的程序没什么实际的价值,连接建立后一般会有大量的事情需要处理,因此让我们来给客户端做出点回应吧。

sendall 函数可通过 Socket 给客户端发送数据:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

import socket

import sys

HOST = ‘‘   # Symbolic name meaning all available interfaces

PORT = 8888 # Arbitrary non-privileged port

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

print ‘Socket created‘

try:

    s.bind((HOST, PORT))

except socket.error , msg:

    print ‘Bind failed. Error Code : ‘ + str(msg[0]) + ‘ Message ‘ + msg[1]

    sys.exit()

    

print ‘Socket bind complete‘

s.listen(10)

print ‘Socket now listening‘

#wait to accept a connection - blocking call

conn, addr = s.accept()

print ‘Connected with ‘ + addr[0] + ‘:‘ + str(addr[1])

#now keep talking with the client

data = conn.recv(1024)

conn.sendall(data)

conn.close()

s.close()

继续运行上述代码,然后打开另外一个命令行窗口输入下面命令:

?

Code

1

2

3

4

5

6

7

$ telnet localhost 8888

Trying 127.0.0.1...

Connected to localhost.

Escape character is ‘^]‘.

happy

happy

Connection closed by foreign host.

可看到客户端接收到来自服务器端的回应内容。

上面的例子还是一样,服务器端回应后就立即退出了。而一些真正的服务器像 www.oschina.net 是一直在运行的,时刻接受连接请求。

也就是说服务器端应该一直处于运行状态,否则就不能成为“服务”,因此我们要让服务器端一直运行,最简单的方法就是把 accept 方法放在一个循环内。

一直在运行的服务器

对上述代码稍作改动:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

import socket

import sys

HOST = ‘‘   # Symbolic name meaning all available interfaces

PORT = 8888 # Arbitrary non-privileged port

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

print ‘Socket created‘

try:

    s.bind((HOST, PORT))

except socket.error , msg:

    print ‘Bind failed. Error Code : ‘ + str(msg[0]) + ‘ Message ‘ + msg[1]

    sys.exit()

    

print ‘Socket bind complete‘

s.listen(10)

print ‘Socket now listening‘

#now keep talking with the client

while 1:

    #wait to accept a connection - blocking call

    conn, addr = s.accept()

    print ‘Connected with ‘ + addr[0] + ‘:‘ + str(addr[1])

    

    data = conn.recv(1024)

    reply = ‘OK...‘ + data

    if not data:

        break

    

    conn.sendall(reply)

conn.close()

s.close()

很简单只是加多一个 while 1 语句而已。

继续运行服务器,然后打开另外三个命令行窗口。每个窗口都使用 telnet 命令连接到服务器:

?


1

2

3

4

5

6

7

$ telnet localhost 5000

Trying 127.0.0.1...

Connected to localhost.

Escape character is ‘^]‘.

happy

OK .. happy

Connection closed by foreign host.

服务器所在的终端窗口显示的是:

?


1

2

3

4

5

6

7

$ python server.py

Socket created

Socket bind complete

Socket now listening

Connected with 127.0.0.1:60225

Connected with 127.0.0.1:60237

Connected with 127.0.0.1:60239

你看服务器再也不退出了,好吧,用 Ctrl+C 关闭服务器,所有的 telnet 终端将会显示 "Connection closed by foreign host."

已经很不错了,但是这样的通讯效率太低了,服务器程序使用循环来接受连接并发送回应,这相当于是一次最多处理一个客户端的请求,而我们要求服务器可同时处理多个请求。

处理多个连接

为了处理多个连接,我们需要一个独立的处理代码在主服务器接收到连接时运行。一种方法是使用线程,服务器接收到连接然后创建一个线程来处理连接收发数据,然后主服务器程序返回去接收新的连接。

下面是我们使用线程来处理连接请求:

?

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

import socket

import sys

from thread import *

HOST = ‘‘   # Symbolic name meaning all available interfaces

PORT = 8888 # Arbitrary non-privileged port

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

print ‘Socket created‘

#Bind socket to local host and port

try:

    s.bind((HOST, PORT))

except socket.error , msg:

    print ‘Bind failed. Error Code : ‘ + str(msg[0]) + ‘ Message ‘ + msg[1]

    sys.exit()

    

print ‘Socket bind complete‘

#Start listening on socket

s.listen(10)

print ‘Socket now listening‘

#Function for handling connections. This will be used to create threads

def clientthread(conn):

    #Sending message to connected client

    conn.send(‘Welcome to the server. Type something and hit enter\n‘) #send only takes string

    

    #infinite loop so that function do not terminate and thread do not end.

    while True:

        

        #Receiving from client

        data = conn.recv(1024)

        reply = ‘OK...‘ + data

        if not data:

            break

    

        conn.sendall(reply)

    

    #came out of loop

    conn.close()

#now keep talking with the client

while 1:

    #wait to accept a connection - blocking call

    conn, addr = s.accept()

    print ‘Connected with ‘ + addr[0] + ‘:‘ + str(addr[1])

    

    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.

    start_new_thread(clientthread ,(conn,))

s.close()

运行上述服务端程序,然后像之前一样打开三个终端窗口并执行 telent 命令:

?


1

2

3

4

5

6

7

8

9

10

11

$ telnet localhost 8888

Trying 127.0.0.1...

Connected to localhost.

Escape character is ‘^]‘.

Welcome to the server. Type something and hit enter

hi

OK...hi

asd

OK...asd

cv

OK...cv

服务器端所在终端窗口输出信息如下:

?


1

2

3

4

5

6

$ python server.py

Socket created

Socket bind complete

Socket now listening

Connected with 127.0.0.1:60730

Connected with 127.0.0.1:60731

线程接管了连接并返回相应数据给客户端。

这便是我们所要介绍的服务器端编程。

结论

到这里为止,你已经学习了 Python 的 Socket 基本编程,你可自己动手编写一些例子来强化这些知识。

你可能会遇见一些问题:Bind failed. Error Code : 98 Message Address already in use,碰见这种问题只需要简单更改服务器端口即可。

时间: 2024-08-06 07:34:19

Python 的 Socket 编程教程的相关文章

Python 3 socket编程

Python 3 socket编程 一 客户端/服务器架构 互联网中处处是C/S架构 1.C/S结构,即Client/Server(客户端/服务器)结构 2.在互联网中处处可见c/s架构 比如说浏览器,在线视频,各种社交软件. C/S架构与socket的关系: 我们学习socket就是为了c/s架构的开发 学习socket一定要先学习互联网协议: 1.如何基于socket编程,来开发一款自己的C/S架构软件 2..C/S架构的软件(软件属于应用层)是基于网络进行通信的 3.网络的核心即一堆协议,

【python】socket编程常量错误问题-1 'AF_INET'错误

1 2 3 4 5 6 7 8 9 # -*- coding: utf-8 -*- import socket print "Creating socket" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  print "Done!" print "Connecting to remote host..." s.connect(("www.baidu.com", 8

Python异步Socket编程

异步网络据说能极大的提高网络server的连接速度,所以打算写一个专题,来学习和了解异步网络.因为Python有个非常出名的异步Lib:Twisted,所以就用Python来完成. OK,首先写一个pythone socket的server段,对开放三个端口:10000,10001,10002.krondo的例子中是每个server绑定一个端口,测试的时候需要分别开3个shell,分别运行.这太麻烦了,就分别用三个Thread来运行这些services. Java代码   import optp

Python基础-socket编程

一.网络编程 自从互联网诞生以来,现在基本上所有的程序都是网络程序,很少有单机版的程序了. 计算机网络就是把各个计算机连接到一起,让网络中的计算机可以互相通信.网络编程就是如何在程序中实现两台计算机的通信. 举个例子,当你使用浏览器访问新浪网时,你的计算机就和新浪的某台服务器通过互联网连接起来了,然后,新浪的服务器把网页内容作为数据通过互联网传输到你的电脑上. 由于你的电脑上可能不止浏览器,还有QQ.Skype.Dropbox.邮件客户端等,不同的程序连接的别的计算机也会不同,所以,更确切地说,

python之socket编程

一.概述 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. socket起源于Unix,而Unix/Linux基本哲学之一就是"一切皆文件",对于文件用[打开][读写][关闭]模式来操作.socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO.打开.关闭) socket和file的区别: file模

Python 006- python socket编程详细介绍

转自https://blog.csdn.net/rebelqsp/article/details/22109925 Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供了标准的 BSD Sockets API. 第二个是 SocketServer, 它提供了服务器中心类,可以简化网络服务器的开发. 下面讲的是Socket模块功能 1.Socket 类型 套接字格式: socket(family,type[,protocal]) 使用给定的地址族.套接字类型.协议编

Python中socket编程

1.Socket介绍: Python中提供socket.py标准库,非常底层的接口库. Socket是一种通用的网络编程接口,和网络层次没有一一对应关系. 跨操作系统的.紧密结合tcp和udp来使用的. 接口简单,但是背后的原理不简单,ip加tcp,通过插两端.通过socket通道:连接程序. 建立关联. apc库. 加端口是因为应用程序太多了.绑定ip地址,作为假端口. 端口是由谁管理的 一般都是tcp和udp编程.Socket基本的,chatserver. 协议族:AF 表示address

python异步socket编程之一

异步网络能极大地提高程序的并行处理能力,所以写了一个专题来总结python中的异步通信. 一.同步client与同步server的通信 1.1. <python的socket通信实例>中的例子 1. TCP server端代码 #!/usr/bin/env python # # -*- coding:utf-8 -*- # File: sync_socket_server.py # from socket import * from time import ctime HOST = '' PO

Python的socket编程

目前处在学习python的阶段,昨天看到了python的socket模块,分别实现TCP.UDP时间戳回显. 1.tcp通信server和client代码 # tcpServer.py #!/usr/bin/python # -*- coding: utf-8 -*- from socket import * from time import ctime HOST = '' PORT = 21156 BUFSIZE = 1024 ADDR = (HOST,PORT) tcpServerSock