還是那個題目(題目和流程見java版本),感覺光用java寫一點新意也沒有,恰巧剛學習了python,何不拿來一用,呵呵:
服務器端:
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
53
54
55
56
57
58
|
import SocketServer, time class MyServer(SocketServer.BaseRequestHandler): userInfo = { 'yangsq' : 'yangsq' , 'hudeyong' : 'hudeyong' , 'mudan' : 'mudan' } def handle( self ): print 'Connected from' , self .client_address while True : receivedData = self .request.recv( 8192 ) if not receivedData: continue elif receivedData = = 'Hi, server' : self .request.sendall( 'hi, client' ) elif receivedData.startswith( 'name' ): self .clientName = receivedData.split( ':' )[ - 1 ] if MyServer.userInfo.has_key( self .clientName): self .request.sendall( 'valid' ) else : self .request.sendall( 'invalid' ) elif receivedData.startswith( 'pwd' ): self .clientPwd = receivedData.split( ':' )[ - 1 ] if self .clientPwd = = MyServer.userInfo[ self .clientName]: self .request.sendall( 'valid' ) time.sleep( 5 ) sfile = open ( 'PyNet.pdf' , 'rb' ) while True : data = sfile.read( 1024 ) if not data: break while len (data) > 0 : intSent = self .request.send(data) data = data[intSent:] time.sleep( 3 ) self .request.sendall( 'EOF' ) else : self .request.sendall( 'invalid' ) elif receivedData = = 'bye' : break self .request.close() print 'Disconnected from' , self .client_address print if __name__ = = '__main__' : print 'Server is started\nwaiting for connection...\n' srv = SocketServer.ThreadingTCPServer(( 'localhost' , 50000 ), MyServer) srv.serve_forever() |
說明:
line-55到line-58的作用就相當于java中某個類里面的main函數,即一個類的入口。
python中SocketServer module里提供了好多實用的現成的類,BaseRequestHandler就是一個,它的作用是為每一個請求fork一個線程,只要繼承它,就有這個能力了,哈哈,真是美事。
當然,我們繼承了BaseRequestHandler,就是override它的handle方法,就像java中繼承了Thread后要實現run方法一樣。實際上這個handle方法的內容和我們的java版本的run函數實現的完全一樣。
line-30到line-43就是處理文件下載的主要內容了。看著都挺眼熟的呵:)
這里在文件發送完后發了一個“EOF”,告訴client文件傳完了。
客戶端:
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
53
54
55
56
|
import socket, time class MyClient: def __init__( self ): print 'Prepare for connecting...' def connect( self ): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(( 'localhost' , 50000 )) sock.sendall( 'Hi, server' ) self .response = sock.recv( 8192 ) print 'Server:' , self .response self .s = raw_input ( "Server: Do you want get the 'thinking in python' file?(y/n):" ) if self .s = = 'y' : while True : self .name = raw_input ( 'Server: input our name:' ) sock.sendall( 'name:' + self .name.strip()) self .response = sock.recv( 8192 ) if self .response = = 'valid' : break else : print 'Server: Invalid username' while True : self .pwd = raw_input ( 'Server: input our password:' ) sock.sendall( 'pwd:' + self .pwd.strip()) self .response = sock.recv( 8192 ) if self .response = = 'valid' : print 'please wait...' f = open ( 'b.pdf' , 'wb' ) while True : data = sock.recv( 1024 ) if data = = 'EOF' : break f.write(data) f.flush() f.close() print 'download finished' break else : print 'Server: Invalid password' sock.sendall( 'bye' ) sock.close() print 'Disconnected' if __name__ = = '__main__' : client = MyClient() client.connect() |
line-34到line-41處理文件下載,client收到server的“EOF”信號后,就知道文件傳完了。
最后需要說明一下python的文件,由于是內置類型,所以不想java那樣有那么多的reader,writer,input,ouput啊。python中,在打開或建立一個文件時,主要是通過模式(mode)來區別的。
python的網絡編程確實簡單,因為它提供了各種功能的已經寫好的類,直接繼承就Ok了。
python還在學習中,上面的例子跑通是沒問題,但寫得肯定不夠好,還得學習啊