Python 簡單socket

來源:互聯網
上載者:User

標籤:add   服務端   物理層   res   enc   inter   decode   nec   pre   

OSI七層模型(Open System Interconnection,開放式系統互聯)

 

應用程式層展示層回話層傳輸層     tcp,udp網路層     ip,icmp資料連結層  mac地址物理層     物理網卡

  

 

服務端:

import socket#執行個體化,綁定,監聽,等待,解構(標識,ip-port),接收,回應,關閉server = socket.socket()server.bind((‘localhost‘,6969))server.listen()print(‘開始監聽...‘)conn,addr = server.accept()print(conn,addr)data = conn.recv(1024)print(‘服務端接收到資料:‘,data.decode())conn.send(‘Hi, i am the server.‘.encode())server.close()

  

用戶端:

import socket#執行個體化,串連,發送,接收回應,關閉client = socket.socket()client.connect((‘localhost‘,6969))client.send(‘Hi,i am the client.‘.encode())print(‘發送資料...‘)data = client.recv(1024)print(‘用戶端接收到資料:‘,data.decode())client.close()

  

1. 先運行服務端,監聽連接埠,開始等待傳入資料:

開始監聽...

 

2. 啟動用戶端,發送資料,接收響應:

發送資料...用戶端接收到資料: Hi, i am the server.

  

3. 回到服務端,查看接收到的資料:

開始監聽...<socket.socket fd=6, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=(‘127.0.0.1‘, 6969), raddr=(‘127.0.0.1‘, 55149)> (‘127.0.0.1‘, 55149)接收到的資料: Hi,i am the client.

  

 

Python 簡單socket

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.