qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Python Socket Client及Server簡單編程

            本文主要介紹使用Python語言編寫Socket協議Server及Client的簡單實現方法。
            1. Python Socket編程簡介
            Socket通常也稱作"套接字",應用程序通常通過"套接字"向網絡發出請求或者應答網絡請求。
            三種流行的套接字類型是:stream,datagram和raw。stream和datagram套接字可以直接與TCP協議進行接口,而raw套接字則接口到IP協議。
            Python Socket模塊提供了對低層BSD套接字樣式網絡的訪問,使用該模塊建立具有TCP和流套接字的簡單服務器。詳見https://docs.python.org/2/library/socket.html
            2. Python Socket Server
            實現代碼如下
          # -*- coding:utf-8 -*-
          from socket import *
          def SocketServer():
          try:
          Colon = ServerUrl.find(':')
          IP = ServerUrl[0:Colon]
          Port = int(ServerUrl[Colon+1:])
          #建立socket對象
          print 'Server start:%s'%ServerUrl
          sockobj = socket(AF_INET, SOCK_STREAM)
          sockobj.setsockopt(SOL_SOCKET,SO_REUSEADDR, 1)
          #綁定IP端口號
          sockobj.bind((IP, Port))
          #監聽,允許5個連結
          sockobj.listen(5)
          #直到進程結束時才結束循環
          while True:
          #等待client連結
          connection, address = sockobj.accept( )
          print 'Server connected by client:', address
          while True:
          #讀取Client消息包內容
          data = connection.recv(1024)
          #如果沒有data,跳出循環
          if not data: break
          #發送回復至Client
          RES='200 OK'
          connection.send(RES)
          print 'Receive MSG:%s'%data.strip()
          print 'Send RES:%s\r\n'%RES
          #關閉Socket
          connection.close( )
          except Exception,ex:
          print ex
          ServerUrl = "192.168.16.15:9999"
          SocketServer()
            注:需要注意的是Socket對象建立后需要加上sockobj.setsockopt(SOL_SOCKET,SO_REUSEADDR, 1),否則會出現Python腳本重啟后Socket Server端口不會立刻關閉,出現端口占用錯誤。
            3. Python Socket Client
            實現代碼如下
          # -*- coding:utf-8 -*-
          from socket import *
          def SocketClient():
          try:
          #建立socket對象
          s=socket(AF_INET,SOCK_STREAM,0)
          Colon = ServerUrl.find(':')
          IP = ServerUrl[0:Colon]
          Port = ServerUrl[Colon+1:]
          #建立連接
          s.connect((IP,int(Port)))
          sdata='GET /Test HTTP/1.1\r\n\
          Host: %s\r\n\r\n'%ServerUrl
          print "Request:\r\n%s\r\n"%sdata
          s.send(sdata)
          sresult=s.recv(1024)
          print "Response:\r\n%s\r\n" %sresult
          #關閉Socket
          s.close()
          except Exception,ex:
          print ex
          ServerUrl = "192.168.16.15:9999"
          SocketClient()
            4. 運行結果
            Socket Server端運行截圖如下:
            Socket-Server
            Socket Client端運行截圖如下:

          posted on 2014-07-15 10:17 順其自然EVO 閱讀(1274) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 措勤县| 无极县| 遂平县| 洱源县| 吐鲁番市| 靖西县| 休宁县| 社旗县| 洛隆县| 扎兰屯市| 元谋县| 中超| 涞水县| 吉木萨尔县| 双城市| 定襄县| 罗源县| 灯塔市| 临漳县| 建水县| 龙游县| 西城区| 贺州市| 青州市| 调兵山市| 腾冲县| 泗水县| 曲松县| 保靖县| 永年县| 奉新县| 班戈县| 固安县| 无棣县| 德阳市| 乌鲁木齐市| 东明县| 长葛市| 彭山县| 通江县| 广元市|