python-using the socket client server to loop and send messages

Source: Internet
Author: User

ImportSocket#From socket Import *Ip_port=('127.0.0.1', 8000) Back_log=5buffer_size=1024Tcp_server=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) Tcp_server.bind (ip_port) Tcp_server.listen (back_log)Print('start running on the service side! ') conn,addr=tcp_server.accept ()Print('Two-way connection:', conn)Print('Client Address:', addr) whileTrue:data=conn.recv (buffer_size)Print('messages from the client:', Data.decode ('Utf-8')) msg=input ('service-side input >>>:') Conn.send (Msg.encode ('Utf-8')) Conn.close () tcp_server.close ()
Service Side
ImportSocket#From socket Import *Ip_port= ('127.0.0.1', 8000) Buffer_size=1024tcp_client=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) Tcp_client.connect (ip_port) whiletrue:msg=input ('client input >>>:'). Strip () Tcp_client.send (Msg.encode ('Utf-8'))    Print('The client has sent a message!') Data=tcp_client.recv (buffer_size)Print('information from the service side:', Data.decode ('Utf-8') ) Tcp_client.close ()
Client

python-using the socket client server to loop and send messages

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.