Python3 network programming,

Source: Internet
Author: User

Python3 network programming,


Network Programming (Definition)
The essence of network programming is data exchange between two devices. Of course, in computer networks, devices mainly refer to computers and data transmission itself.
It is not difficult to send data from one device to one device, and then receive feedback from the other device.
Network Programming is basically based on the request/response method, that is, a device sends request data to another device, and then receives feedback from another device.
IP address and domain name
In real life, if you want to make a phone call, you need to know the phone number of the corresponding person. If you want to send a mail, you need to know the recipient's address. In the network
Also, you need to know the location of a device. You need to use the IP address of the device. The specific connection process is implemented by hardware. Generally IPv4
Port Concept
In order to run multiple programs on a device, the concept of port is designed manually, just like the company's extension number.
It is required that a device has 216 or 65536 ports. Each port corresponds to a unique program.


For python network programming, a socket (called socket) is required)
Enter cmd and enter pip install socket to download the latest version)
I have introduced so much about network programming. I will write an instance below.
# Create a caller (encoding required for sending characters) import socketimport threading # create a client socket c = socket. socket (socket. AF_INET, socket. SOCK_STREAM) print ('create client socket ') c. connect ('00. 00.00.00 ', 0000) # link to the other party. If an exception (your ip address) is not thrown, print (' I already linked to Service') # send the sent data to encode () encoding def myreve (c): while True: # 1024b = 1 k # 3b = 1 byte (so the character is less than 300) msg = c. recv (1024) # block print (msg. decode () # decode threading. _ start_new_thread (myreve, (c,) while True: msg = input () c. send (msg. encode () # code # called Party (similar to the caller) ss = socket. socket (socket. AF_INET, socket. SOCK_STREAM) print ('establish a service socket ') # bind a service socket to the port number and host print ('Bind port and host') ss. bind ('localhost', 9999) # maximum number of established connections, exceeding the number of queued ss. listen (5) # accept client socket request c = ss. accept () print ('user linked to me ') # thread function def myreve (c): while True: msg = c. recv (1024) # block print (msg. decode () # decode threading. _ start_new_thread (myreve, (c [0],) while True: msg = input () c [0]. send (msg. encode () # Encoding

 

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.