Python Network programming Start (socket send message) _python

Source: Internet
Author: User
One, service side (server.py)
The thing to do for the service side is:
1. Create a Socket object
Import Socket
s = socket.socket (socket.af_inet, socket. SOCK_DGRAM)
2. Bind a Port
S.bind (("", 8081))
3. Accept messages from the client
While True:
# Receive up to 1,024 bytes in a datagram
data, addr = S.recvfrom (1024)
print "Received:", data, "from",addr
Second, the client (client.py)
What the client is going to do is:
1. Create a Socket object.
Import Socket
s = socket.socket (socket.af_inet, socket. SOCK_DGRAM)
2. Send a message to a specified port on a server. Because UDP is used, packets will be discarded if the server side is not received.
Port = 8081
Host = "localhost"
While True:
msg = raw_input ()
S.sendto (MSG, (host, Port))
Third, the operation tries

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.