Python Learning--socket Module

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.