python socket網路編程,pythonsocket

來源:互聯網
上載者:User

python socket網路編程,pythonsocket

socket通訊端:為了使2個不同應用程式進行通訊。

兩種通訊端類型:基於檔案的和基於網路的

1.基於檔案(AF_LOCAL或AF_UNIX):代表Unix

2.面向網路(AF_INET):代表網際網路

連線導向的通訊端(SOCK_STREAM):TCP

面向不需連線的通訊端(SOCK_DGRAM):UDP

Python中的網路編程socket()模組

 

TCP 網路程式

serice服務端

#!/usr/bin/env python# -*- coding: utf-8 -*-from socket import *from time import ctimeHOST = ''  ##為空白表示bind()函數可以綁定在所有有效地址上PORT=21567BUFSIZ = 1024  ##緩衝區大小  1KADDR = (HOST,PORT)tcpSerSock = socket(AF_INET,SOCK_STREAM)tcpSerSock.bind(ADDR)tcpSerSock.listen(5)  ##允許最多多少個連結 後來的會被拒絕掉while True:    print 'waiting for connection...'    tcpCliSock,addr = tcpSerSock.accept()  ##阻塞式線程    print '...connected from:',addr    while True:        data = tcpCliSock.recv(BUFSIZ)
##阻塞式線程
if not data: break tcpCliSock.send('(%s) %s' % (ctime(),data)) tcpCliSock.close() tcpSerSock.close()

client 用戶端

 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3  4 from socket import * 5 HOST = 'localhost'  ## 6 PORT=21517 7 BUFSIZ = 1024  ##緩衝區大小  1K 8 ADDR = (HOST,PORT) 9 10 tcpCliSock = socket(AF_INET,SOCK_STREAM)11 tcpCliSock.connect(ADDR)12 13 while True:14     data = raw_input('> ')15     if not data:16         break17     tcpCliSock.send(data)18     data = tcpCliSock.recv(BUFSIZ)19     if not data:20         break21     print data22 tcpCliSock.close()

UDP程式 

service 服務端

 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3  4 from socket import * 5 from time import ctime 6  7 HOST='' 8 PORT=21567 9 BUFSIZ = 102410 ADDR = (HOST,PORT)11 12 udpSerSock = socket(AF_INET,SOCK_DGRAM)13 udpSerSock.bind(ADDR)14 15 while True:16     print 'waiting for message....'17     data , addr = udpSerSock.recvfrom(BUFSIZ)18     udpSerSock.sendto('[%s] %s' % (ctime(),data),addr)19     print '...received from and returned to:',addr20 udpSerSock.close()
View Code

client 用戶端

 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3  4 from socket import * 5  6 HOST='localhost' 7 PORT=21567 8 BUFSIZ=1024 9 ADDR = (HOST,PORT)10 11 udpCliSock= socket(AF_INET,SOCK_DGRAM)12 13 while True:14     data = raw_input('> ')15     if not data:16         break17     udpCliSock.sendto(data,ADDR)18     data,ADDR = udpCliSock.recvfrom(BUFSIZ)19     if not data:20         break21     print data22 23 udpCliSock.close()
View Code

SocketService 通訊程式

服務端

 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3  4 from SocketServer import (TCPServer as TCP,StreamRequestHandler as SRH) 5 from time import ctime 6  7 HOST = '' 8 PORT = 21569 9 ADDR = (HOST,PORT)10 11 class MyRequestHandler(SRH):12     def handle(self):13         print '...connected from:',self.client_address14         self.wfile.write('[%s] %s' % (ctime(),self.rfile.readline()))15 16 tcpServ = TCP(ADDR,MyRequestHandler)17 print 'waiting for connection...'18 tcpServ.serve_forever()
View Code

用戶端

 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3  4 from socket import * 5 HOST = 'localhost'  ## 6 # HOST = '192.168.0.51' 7 PORT=21569 8 BUFSIZ = 1024  ##緩衝區大小  1K 9 ADDR = (HOST,PORT)10 11 while True:12     tcpCliSock = socket(AF_INET, SOCK_STREAM)13     tcpCliSock.connect(ADDR)14     data = raw_input('> ')15     if not data:16         break17     tcpCliSock.send('%s\r\n' % data)18     data = tcpCliSock.recv(BUFSIZ)19     if not data:20         break21     print data.strip()22     tcpCliSock.close()
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.