Server side:
Import Socket #导入socket模块s = Socket.socket (socket.af_inet,socket. SOCK_STREAM) host = "Port = 6074s.bind ((host,port)) #绑定主机和端口s. Listen (5) #监听传入连接, the maximum number of connections that the operating system can suspend CONN,ADDR=S.ACCE PT () #接受连接并返回 (CONN,ADDR) conn is used to receive and send data, addr is the connection client address print (' Connected by ', addr) #打印客户端地址while 1:data = CONN.R ECV (1024x768) #接收数据 if not data:break conn.sendall (data) #返回接收到的数据conn. Close ()
Client side:
Import sockets = Socket.socket (Socket.af_inet,socket. SOCK_STREAM) host = "www.xiaoran.com" #服务器地址port = 6074 #服务器端口s. Connect ((host,port)) #连接服务器s. Sendall ("Hello World ") #将数据发送最大的连接套接字, but attempts to send all data before returning. Successful return none, Failure throws exception. data = S.RECV (1024x768) #接收套接字的数据, which is returned as a string. S.close () #关闭套接字print (' OK '), repr (data) #repr () converts the object to a string display, just for display (RELATED links
This article is from the "Autumn Breeze" blog, please be sure to keep this source http://aitiankong.blog.51cto.com/8923090/1564339
"Learning notes" python network programming (i)