"Python" socket

Source: Internet
Author: User

Udp

udp_server.py

 fromDatetimeImportdatetimeImportsocketserver_address= ('localhost', 6789) max_size= 4096Print('starting the server at', DateTime.Now ())Print('waiting for a client-to-call.') Server=Socket.socket (socket.af_inet, socket. SOCK_DGRAM) Server.bind (server_address) data, client=Server.recvfrom (max_size)Print(' at', DateTime.Now (), client,'said', data) server.sendto (b'is talking to me?', client) server.close ()

udp_client.py

ImportSocket fromDatetimeImportdatetimeserver_address= ('localhost', 6789) max_size= 4096Print('starting the client at', DateTime.Now ()) client=Socket.socket (socket.af_inet, socket. SOCK_DGRAM) Client.sendto (b'hey!', server_address) data, server=Client.recvfrom (max_size)Print(' at', DateTime.Now (), Server,'said', data) client.close ()

Tcp

tcp_server.py

 fromDatetimeImportdatetimeImportsocketaddress= ('localhost', 6789) max_size= 1000Print('starting the server at', DateTime.Now ())Print('waiting for a client-to-call.') Server=Socket.socket (socket.af_inet, socket. SOCK_STREAM) Server.bind (address) Server.listen (5)#A maximum of 5 client connections, and more than 5 will be rejectedClient, addr=server.accept () data=client.recv (max_size)Print(' at', DateTime.Now (), client,'said', data) Client.sendall (b'is talking to me?') Client.close () server.close ()

tcp_client.py

ImportSocket fromDatetimeImportdatetimeaddress= ('localhost', 6789) max_size= 1000Print('starting the client at', DateTime.Now ()) client=Socket.socket (socket.af_inet, socket. SOCK_STREAM) Client.connect (address) Client.sendall (b'hey!') Data=client.recv (max_size)Print(' at', DateTime.Now (),'someone replied', data) client.close ()

"Python" socket

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.