python socket程式設計

2021-09-09 08:55:08 字數 733 閱讀 4768

匯入模組

import socket

建立套接字

socket()

繫結伺服器ip和埠號,此處傳參是乙個元組(host, port)

bind((host, port))

設為監聽套接字,並建立監聽佇列

accept()

連線伺服器,此處的引數也是元組

connect((host, port))

server:

import socket

s = socket.socket()

host = socket.gethostname()

port = 12345

s.bind((host,port))

s.listen(5)

while true:

c,addr = s.accept()

print("連線位址",addr)

c.send("hello world")

c.close()

client:

import socket

s = socket.socket()

host = socket.gethostname()

port = 12345

s.connect((host,port))

print(s.recv(1024))

s.close()

Python Socket 程式設計

client import socket,sys if name main 處理引數 argv sys.argv if len argv 3 or len argv 2 and argv 1 print useage argv 0 sys.exit 0 host argv 1 server ip位址...

python socket程式設計

python 編寫server的步驟 1.第一步是建立socket物件。呼叫socket建構函式。如 socket socket.socket family,type family引數代表位址家族,可為af inet或af unix。af inet家族包括internet位址,af unix家族用於...

python socket程式設計

客戶端 author lenovo fromsocketimport host localhost port 2157 bufsize 1024 addr host,port tcpclient socket af inet,sock stream tcpclient.connect addr wh...