Python Advanced---Socket programming in Python (i)

Source: Internet
Author: User

Initial knowledge of socket programming

First, preface

The socket is based on the programming model of the C\S architecture (client \ Server), which is present in Python as a socket module.

A socket is an 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, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.

So, we do not need to understand the TCP/UDP protocol, the socket has been packaged for us, we only need to follow the socket rules to program, write the program is naturally follow the TCP/UDP standard.

Second, socket type

Socket format:

Socket (Family,type[,protocal]) creates a socket with the given address family, socket type, protocol number (default = 0).

Second, the socket function

Note the point:

1) When TCP sends data, a TCP connection is established, so you do not need to specify an address. UDP is for non-connected, each time it is sent to specify who to send.

2) server and client cannot send list, tuple, dictionary directly. Requires string repr (data)

3, Socket programming ideas

TCP Service side:

1 creating sockets, binding sockets to local IP and ports

# Socket.socket (Socket.af_inet,socket. SOCK_STREAM), S.bind ()

2 Start listening for connection #s. Listen ()

3 Enter the loop and continuously accept the client's connection request #s. Accept ()

4 then receive the incoming data, and send the data #s. recv (), S.sendall ()

5 When the transfer is complete, close the socket #s. Close ()

TCP Client:

1 creating sockets, connecting remote addresses

# Socket.socket (Socket.af_inet,socket. SOCK_STREAM), S.connect ()

2 Send data and receive data after connection # S.sendall (), S.RECV ()

3 When the transfer is complete, close the socket #s. Close ()

UDP Service Side

SS = Socket (Socket.af_inet,socket. SOCK_DGRAM)   #创建一个服务器的套接字ss. bind ()       #绑定服务器套接字inf_loop:       #服务器无限循环     cs = Ss.recvfrom ()/ss.sendto () # Dialog ( Receive and send) Ss.close ()                         # Close Server sockets

UDP client

CS = socket (socket.af_inet,socket. SOCK_DGRAM)   # Create client socket Comm_loop:      # Communication Loop    Cs.sendto ()/cs.recvfrom ()   # Dialog (send/Receive) Cs.close ()                      # Close client sockets

Python Advanced---Socket programming in Python (i)

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.