Python Network Programming Learning "one"

Source: Internet
Author: User

Recently, just after the internship, very much like the experimental building, but their own direction is still not sure, I think the space is very large, although already a senior person, think of others are busy to buy professional clothes, buy high-heeled shoes interview, learn makeup, look at themselves, instead began to slowly focus on sports, diet and skin care, haha, The programmer is Jiangzi, the body is the capital of revolution. Now the direction is at least determined, the data analysis, but the image processing this project since the original choice when the responsible, now also responsible AH; So at present learning network programming, but also because seniors want to "wind" landing above the C/s architecture, then I will try it, maybe, You can also pinch to quantify your investment or network. Nonsense not to say, into the learning state.

    1. What is the C/s architecture?

' C ' is ' client ', ' s ' is ' server ', then the initial understanding is to achieve the server and client communication between, then I study the purpose is to achieve the million database above the multi-login, that is, in the server log on, in a number of clients can implement trading operations, The premise is that as long as the local area network to access the server address.

2. What is a socket?

For example, if the million financial data platform is a company I want to visit "million", then I would like to call the company's data or something, this time, the server is the company's operator, a number of telephone is the client, I can access the company's number and port through multiple phone calls to contact the company, this time the phone "socket" is "socket", there are two types of sockets, one is file-based, one is network-based, wherein, when two processes are running on the same computer, This time between the two processes of communication between the socket is based on file type; Well, the network programming we are studying is based on the network type, the family name is "Af_inet", the full name is "address family: The Internet", it is conceivable that the socket address is composed of "host" and "Port" , communication is based on the premise that your phone number and area code are right, and the other company is always ready to answer the phone, that is, you have to have a communication with each other: the address and the same port number, and the other party at any time "listen."

Here, the host address needless to say, then the port number is generally human-given, the legal port number range is 0-65535. Where the port number less than 1024 is the system reserved port, you can view the reserved port number and the corresponding service and socket type by viewing the file "/etc/services" on the Linux system.

3. What is connection-oriented?

Well, now that you have the phone, with the operator, with the socket, there is a lack of a telephone line, more accurate is the connection between your phone and the company's telephone line between, is a connection-oriented. Connection-oriented provides sequential, reliable, non-repetitive data transfer with no boundary, meaning data is split into multiple copies, each of which is correctly delivered to the destination, and then stitched together in order to be sent to the other operator. The main protocol for connection-oriented implementation here is Transmission Control Protocol (TCP). When creating a TCP socket, you need to specify that the socket type is "Sock_stream" when it is created, which literally means socket flow.

Speaking so much, mainly around "sockets", in Python mainly using the socket module for network communication, where the function socket () is used to create sockets. The parameters and examples of the socket function can be achieved by entering "help" on the interactive platform, provided the "import socket" is Socket.socket

Obviously, the socket () has three parameters, respectively Family,type and Protol, then family of course is generally "af_inet", type is "Sock_stream", Protol general default value is 0, do not fill, OK other functions in this document also have detailed introduction, such as accept and bind what, everybody so from clever, this English still can understand drip, nonsense not to say, on a few examples, from http://defshine.github.io/ Python-source-learn01.html.

    • The simplest socket communication

server_1.py

1 #Coding=utf-82 __author__='wing1995'3 ImportSocket4 5 #set native address and communication port number, cache space is 1024x7686HOST ='127.0.0.1'7PORT = 88888ADDR =(HOST, PORT)9Buf_size = 1024Ten  One #Sockets , bind socket addresses and start TCP snooping AServer =Socket.socket (socket.af_inet, socket. SOCK_STREAM) - Server.bind (ADDR) -Server.listen (1) the  - #passive wait for the connection and accept the address, waiting for the client to send data, if there is no data to exit the loop and close the connection to exit the server, while the data received successfully -conn, addr =server.accept () - Print("server connected by:", addr) +  while1: -data =conn.recv (buf_size) +     if  notData: A          Break at     Print("Server recv Success") -Conn.close ()

client_1.py

1 #Coding=utf-82 __author__='wing1995'3 ImportSocket4 5 #host name and port number of the communication for the server6HOST ='127.0.0.1'7PORT =88888 9ADDR =(HOST, PORT)TenBuf_size = 1024 One  A #like a server, the difference is that it sends more data and accepts data -Client =Socket.socket (socket.af_inet, socket. SOCK_STREAM) - Client.connect (ADDR) theClient.send ('client Say:hello, my name is wing!') -data =client.recv (buf_size) - ifData: -     Print("Client recv----", data) +Client.close ()

Run client and server separately look at that.

Error:

All right, first dig a hole here, back up.

Python Network Programming Learning "one"

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.