Python chat program instance code sharing

Source: Internet
Author: User

The code is simple. Check the Code directly:
Copy codeThe Code is 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 | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) $ ')
While True:
Ip = raw_input ('input the Server \'s IPv4 address :')
Ip_match = ip_pattern.match (ip)
If ip_match:
Break
C. connect (ip, 33333 ))
While True:
Sms = raw_input ('input the message you 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 ()

Related Article

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.