http://www.cnblogs.com/alex3714/articles/5830365.html What to look for
Socket Sever
#!/usr/bin/env python#-*-coding:utf-8-*-#Author Ian Ying#Mail: [email protected]ImportSocketImportOsserver=socket.socket () Server.bind ('localhost', 6969))#bind is listening portServer.listen (5)#Listening Port whileTrue:Print("I'm going to start waiting for the phone.") conn, addr= Server.accept ()#is the meaning of waiting #Conn is a connection instance that is generated on the server side when the client is connected . Print("the phone is coming.%s"%[conn, addr]) whileTrue:data= CONN.RECV (1024) if notData:Print('client is lost.') Break #res = os.popen (data). Read () #popen就是打开命令执行, read is get results #with open (' filename ', ' R ') as RET: #这两行就 can be used to transfer files. #data = Ret.read () Print('Receive:', data) Conn.send (Data.upper ()) Server.close ()
Socket Client Module
#!/usr/bin/env python#-*-coding:utf-8-*-#Author Ian Ying#Mail: [email protected]Importsocketclient= Socket.socket ()#declares the socket type while generating the socket link objectClient.connect (('localhost', 6969))#localhost is the native address . whiletrue:msg= Input ('input msg >>:'). Strip ()ifLen (msg) = = 0:Continue #Check msg information to prevent no input information #Client.send (b "Hello, world!") #发送信息Client.send (Msg.encode ('Utf-8')) Data= CLIENT.RECV (1024)#The default accepts 1024 bytes, which is 1k #with open (' filename ', ' W ') as RET: # These two lines can be used to transfer files. #ret = data.write () Print(Data.decode ()) Client.close ()#Close Port
Python Learning--socket Module