Python Eighth day

Source: Internet
Author: User

What is--socket?
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, the socket 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.
Some people also say that the socket is used to identify the location of a host in the Internet, and port is used to identify an application on this machine, the IP address is configured on the network card, and the port is the application Ip+port,ip, The IP-Port binding identifies an application that is unique to the Internet
And the PID of the program is the identity of different processes or threads on the same machine.

--The history and classification of sockets
Sockets originated in the the 1970s UC Berkeley version of Unix, which is what people call BSD Unix. Therefore, sometimes people also refer to sockets as "Berkeley sockets" or "BSD sockets". Initially, sockets are designed to be used for communication between multiple applications on the same host.
This is also called interprocess communication, or IPC. There are two types of sockets (or two races), which are file-based and network-based.
-Socket family 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
-Socket family 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 wide range of address families,
But because we only care about network programming, so most of the time I only use af_inet)

--Socket workflow
A scene in the life. You have to call a friend, dial first, a friend hears a phone call, and then you and your friend establish a connection, you can talk. When the communication is over, hang up the phone and end the conversation. The scene in life explains how this works.
Socket () module function usage
Import socket
Socket.socket (socket_family,socket_type,protocal=0)
Socket_family can be Af_unix or af_inet. Socket_type can be sock_stream or sock_dgram. Protocol is generally not filled, the default value is 0.

Get TCP/IP Sockets
Tcpsock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)

Get UDP/IP sockets
Udpsock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)

Because there are too many properties in the socket module. We made an exception here by using the ' from module import * ' statement. With the ' from socket import * ', we take all the attributes in the socket module into our namespace, which can greatly reduce our code.
For example Tcpsock = socket (af_inet, SOCK_STREAM)

Service-Side socket functions
S.bind () binding (host, port number) to socket
S.listen () Start TCP listener
S.accept () passively accepts a TCP client connection, (blocking) waits for a connection to arrive

Client socket functions
S.connect () Active initialization of TCP server connections
Extended version of the S.CONNECT_EX () connect () function, which returns an error code instead of throwing an exception when an error occurs

Socket functions for public use
S.RECV () Receiving TCP data
S.send () sends TCP data (send data is lost when the amount of data to be sent is greater than the remaining space in the cache)
S.sendall () sends the full TCP data (essentially a cyclic call Send,sendall the data is not lost when the amount of data to be sent is greater than the remaining space in the buffer, and the call to send is sent until it is finished)
S.recvfrom () receiving UDP data
S.sendto () Send UDP data
S.getpeername () The address of the remote that is connected to the current socket
S.getsockname () address of the current socket
S.getsockopt () returns the parameters of the specified socket
S.setsockopt () Sets the parameters of the specified socket
S.close () Close socket

Lock-oriented socket method
S.setblocking () sets the blocking and non-blocking mode for sockets
S.settimeout () Sets the timeout period for blocking socket operations
S.gettimeout () Gets the timeout period for blocking socket operations

Functions for file-oriented sockets
S.fileno () The file descriptor of the socket
S.makefile () Create a file associated with the socket

--TCP-based sockets
TCP is link-based, you must start the server, and then start the client to link the server
TCP Service Side


---service-side------------------------------------------------------------------
#_ *_coding:utf-8_*_

Import socket
ip_port= (' 127.0.0.1 ', 9000) #电话卡
bufsize=1024 #收发消息的尺寸
S=socket.socket (Socket.af_inet,socket. SOCK_STREAM) #买手机
S.bind (Ip_port) #手机插卡
S.listen (5) #手机待机


Conn,addr=s.accept () #手机接电话
# PRINT (conn)
# Print (addr)
Print (' Received phone from%s '%addr[0])

MSG=CONN.RECV (BUFSIZE) #听消息, obedient
Print (Msg,type (msg))

Conn.send (Msg.upper ()) #发消息, Speak

Conn.close () #挂电话

S.close () #手机关机

---client------------------------------------------------------------------
#_ *_coding:utf-8_*_

Import socket
ip_port= (' 127.0.0.1 ', 9000)
bufsize=1024
S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)

S.CONNECT_EX (Ip_port) #拨电话

S.send (' Linhaifeng nb '. Encode (' Utf-8 ')) #发消息, Speak (send byte type only)

FEEDBACK=S.RECV (BUFSIZE) #收消息, obedient
Print (Feedback.decode (' Utf-8 '))

S.close () #挂电话
-----------------------------------------------------------------------------

Python Eighth day

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.