-socket Programming of Python development

Source: Internet
Author: User

I. C/S architecture

C /S is the client/server mode, such as the browser is the client, a servers is the service side, the two for data interaction

Ii.OSI layer Seven

  

Application layer: The application that we use

Transport layer: What you need to know here is the TCP/UDP protocol, which is the encapsulated TCP protocol, which is the port number of the application you're running.

Network layer: The network layer is the processing IP address problem

Data Link Layer: The Ethernet protocol is followed, and the MAC address of the computer is encapsulated

Physical layer: Medium for communication, e.g. twisted pair, radio wave, etc.

The process above can uniquely identify a computer and then complete the communication

Third, Socket Layer

  

Socket layer encapsulates the transport layer, network layer, Data Link layer protocol standard, we only need to follow the socket programming specification can achieve the overall package to achieve the purpose of communication socket

is a The intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode , thesocket is actually a façade mode, which hides the complex TCP/IP protocol family in

after the socket interface,  to the user, a simple set of interfaces is all, let the socket to organize the data, to conform to the specified protocol. so we don't need to understand the TCP/UDP protocol in depth,

the socket has been encapsulated for us. we just have to follow Socke . T's rules go programming, writing programs naturally follow the TCP/UDP standard. We can go through the Socekt to the only

Identifies a computer in the world to complete communication.

Iv. Socket sockets

1. Socket based on file type

    Socket family name: Af_unix

Unix all files, file-based sockets are called by the underlying file system to fetch data, two sockets process running on the same machine, you can access the same file system to complete the communication indirectly

2. Socket based on network type

    Socket family name: af_inet

(There are also af_inet6 used for IPv6 and some other address families, but they are either used only on a platform, or have been discarded, or are rarely used, or are not implemented at all, and Af_inet is the most widely used one in all address families, Python supports a variety of address families, but since we only care about network programming, most of the time I use af_inet only)

3. How socket sockets work

       

       Implement server and client communication

       

Import Socketback_log=5buffer_size=1024ip_port = (' 127.0.0.1 ', 8000) Tcp_server = Socket.socket (socket.af_inet,socket . SOCK_STREAM) Tcp_server.bind (ip_port) Tcp_server.listen (back_log) while True:    conn,addr = tcp_server.accept ()    Print ("bidirectional connection", conn)    print ("Client address", addr) while    True:        try:            data = CONN.RECV (buffer_size)        Except Exception:break        print ("message sent from client", Data.decode (' Utf-8 '))        meg = input (">>>")        Conn.send ( Meg.encode (' Utf-8 '))    conn.close () tcp_server.close ()

  

-socket Programming of Python development

Related Article

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.