網(wǎng)絡(luò)編程的專利權(quán)應(yīng)該屬于Unix,各個(gè)平臺(tái)(如windows、Linux等)、各門語言(C、C++、Python、Java等)所實(shí)現(xiàn)的符合自身特性的語法都大同小異。在我看來,懂得了Unix的socket網(wǎng)絡(luò)編程,其他的形式的網(wǎng)絡(luò)編程方法也就知道了。這句話說得還不太嚴(yán)謹(jǐn)。準(zhǔn)確的應(yīng)該說成懂得了socket編程的原理,網(wǎng)絡(luò)編程也就知道了,不同之處就在于每個(gè)平臺(tái),每個(gè)語言都有自己專享的語法,我們直接靈活套用就行了。
下面是用python實(shí)現(xiàn)的最基本的網(wǎng)絡(luò)編程的例子,即依托于客戶端-服務(wù)器的架構(gòu),實(shí)現(xiàn)客戶端與服務(wù)器之間的單向“數(shù)據(jù)流通”。我們分別用兩個(gè)方法來實(shí)現(xiàn),一個(gè)方法是最原始的socket編程,另一個(gè)方法是利用python的面向?qū)ο髮?duì)第一種方法進(jìn)行封裝實(shí)現(xiàn),目的是減少實(shí)現(xiàn)透明性,便于快速開發(fā)。
要求:客戶端輸入數(shù)據(jù),發(fā)送到服務(wù)端,服務(wù)器端生成(時(shí)間戳+數(shù)據(jù))的封裝數(shù)據(jù)回應(yīng)客戶端。由于socket編程包括兩種:面向連接的和無連接的,這兩種分別對(duì)應(yīng)TCP數(shù)據(jù)流和UDP數(shù)據(jù)報(bào)文。所以,我們兩種方法都進(jìn)行實(shí)現(xiàn)。
一、Python socket編程
面向連接的TCP socket編程:
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
|
# -*- coding: utf- 8 -*- 3 from socket import * from time import ctime # Address and Port HOST = '' PORT = 21567 ADDR = (HOST, PORT) # BuffSize BUFSIZ = 1024 # build socket tcpSerSock = socket(AF_INET, SOCK_STREAM) # bind socket tcpSerSock.bind(ADDR) # listen 5 client tcpSerSock.listen( 5 ) try : while True: print 'waiting for connection...' # build client socket tcpCliSock, addr = tcpSerSock.accept() print '...connect from:' , addr # accept data and process while True: data = tcpCliSock.recv(BUFSIZ) if not data: break tcpCliSock.send( '[%s] %s' % (ctime(), data)) # close client socket tcpCliSock.close() except EOFError, KeyboardInterrupt: tcpSerSock.close() |
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
|
# -*- coding:utf-8 -*- from socket import * # Address and Port HOST = '127.0.0.1' PORT = 21567 ADDR = (HOST, PORT) # BufferSize BUFSIZ = 1024 #build socket tcpCliSocket = socket(AF_INET, SOCK_STREAM) tcpCliSocket.connect(ADDR) while True : data = raw_input ( '> ' ) if not data: break # send data tcpCliSocket.send(data) # recv data data = tcpCliSocket.recv(BUFSIZ) if not data: break # show data print data tcpCliSocket.close() |
無連接的UDP socket編程
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
|
# -*- coding: utf-8 -*- from socket import * from time import ctime # Address and Port HOST = '' PORT = 8000 ADDR = (HOST, PORT) # BufferSize BUFFSIZE = 1024 # build socket udpSerSock = socket(AF_INET, SOCK_DGRAM) # bind socket udpSerSock.bind(ADDR) try : while True : print 'waiting the message...' data, addr = udpSerSock.recvfrom(BUFFSIZE) print 'received the message: ' + data + ' from: ' , addr udpSerSock.sendto( '[%s] %s' % (ctime(), data), addr) except EOFError, KeyboardInterrupt: udpSerSock.close() |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# -*- coding: utf-8 -*- from socket import * # Address and Port HOST = 'localhost' PORT = 8000 ADDR = (HOST, PORT) # BufferSize BUFSIZ = 1024 # build socket udpCliSock = socket(AF_INET, SOCK_DGRAM) while True : data = raw_input ( '> ' ) udpCliSock.sendto(data, ADDR) data = udpCliSock.recvfrom(BUFSIZ) if not data: break print data udpCliSock.close() |
二、基于封裝類SocketServer的網(wǎng)絡(luò)編程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# -*- coding: utf-8 -*- from SocketServer import TCPServer as TCP, StreamRequestHandler as SRH from time import ctime # Address and Port HOST = '' PORT = 21567 ADDR = (HOST, PORT) # BuffSize BUFSIZ = 1024 # build RequestHandler class MyRequestHandler(SRH): def handle( self ): print '...connected from: ' , self .client_address self .wfile.write( '[%s] %s' % (ctime(), self .rfile.readline())) # build TCPServer TCPServ = TCP(ADDR, MyRequestHandler) print 'waiting for connection...' # loop to process TCPServ.serve_forever() |
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
|
# -*- coding:utf-8 -*- from socket import * # Address and Port HOST = '127.0.0.1' PORT = 21567 ADDR = (HOST, PORT) # BufferSize BUFSIZ = 1024 while True : # note: SocketServer 的請(qǐng)求處理器的默認(rèn)行為是接受連接, # 得到請(qǐng)求,然后就關(guān)閉連接,所以需要多次連接 tcpCliSock = socket(AF_INET, SOCK_STREAM) tcpCliSock.connect(ADDR) # process data data = raw_input ( '> ' ) if not data: break tcpCliSock.send( '%s\r\n' % data) data = tcpCliSock.recv(BUFSIZ) if not data: break print data.strip() tcpCliSock.close() |
感謝閱讀,希望能幫助大家,謝謝大家對(duì)本站的支持!
原文鏈接:http://www.cnblogs.com/bakari/p/4213832.html