Python-sokect 示例

server:

#coding=utf-8
import socket
_sokect =socket.socket() #创建sokect
_host   =socket.gethostname() #获取本地主机名
_port   =8090 #设置端口
_sokect.bind((_host,_port))
_sokect.listen(5) #等待客户端连接
while True:
    client,addr =_sokect.accept()
    print('Connection address',addr)
    client.send('Welcome to visit') #中文的话会乱码,应该是需要转码
    client.close() #关闭连接


client:

#coding=utf-8
import socket
_sokect =socket.socket()
_host  =socket.gethostname()
_port =8090
_sokect.connect((_host,_port))
print(_sokect.recv(1024))
_sokect.close()

  

原文地址:https://www.cnblogs.com/ms_senda/p/11957771.html