Python Chat program instance code sharing _python

Source: Internet
Author: User

Code simple, look at the code directly:

Copy Code code as follows:

Import socket
Import threading
Import re
#import Tkinter

Def ser ():
S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
S.bind ((', 33333))
S.listen (1)
Conn,addr=s.accept ()
While True:
print ' [%s:%d] send a message to me:%s '% (ADDR[0],ADDR[1],CONN.RECV (1024))
S.close ()

Def CLT ():
    c=socket.socket (socket.af_inet,socket. SOCK_STREAM)
    ip_pattern=re.compile (R ' ^ (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. ( \D{1,2}|1\D\D|2[0-4]\D|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) $ '
    while True:
         Ip=raw_input (' Input the Server\ ' IPV4 Address: ')
        Ip_match=ip_ Pattern.match (IP)
        if Ip_match:
             Break
    C.connect ((ip,33333))
    While True:
        sms=raw_input (' input, ' want to send: ')
        c.sendall (SMS)
    c.close ()

If __name__== "__main__":
Ser=threading. Thread (Target=ser)
Clt=threading. Thread (TARGET=CLT)
Ser.start ()
Clt.start ()
Ser.join ()
Clt.join ()

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.