Sockets are also commonly referred to as "sockets," which describe IP addresses and ports, and are a handle to a chain of communication, where applications usually make requests to the network through "sockets" or respond to network requests.
Sockets originate from UNIX, and one of the basic philosophies of unix/linux is "Everything is file", and the file is operated with "open" "Read and Write" "Off" mode. Socket is an implementation of this pattern, the socket is a special kind of file, some of the socket function is the operation of it (read/write Io, open, close)
The difference between a socket and file:
The file module is "open", "Read and write" "Close" for a specified document
One. Socket module
Socket, commonly known as socket, is actually a combination of IP address and port. Similar to this form (IP, port), where IP represents a host, port represents an application, we can communicate through a socket and another host.
About the socket source code parsing in the Tarnado series article, is writing in .....
1. Means of communication
TCP Communication
UDP communication
UNIX-based communication
2. How to Socket
# So much for the moment, and then I'll keep on saving methods of socket objects (keyword arguments not allowed): _accept ()--Accept Conne Ction, returning new socket FD and client address bind (addr)--bind the socket to a local address binds a socket socket to the Close ()-The close the socket closes a socket connect (addr)-connect the socket to a remote address connected to the remote host CONNECT_EX (addr)--Connect, return an error code instead of the exception DUP ()--return a new socket FD duplicated from Fileno ( ) Fileno ()--Return underlying file descriptor Getpeername ()--Return remote address [*] getsockname ()--retur n Local address getsockopt (level, optname[, Buflen])--Get socket Options gettimeout ()--return timeout or None Listen ([n])--Start listening for incoming connections recv (buflen[, Flags])--receive data to receive Recv_into (buff er[, nbytes[, flags])--receive data (into a buffer) to receive a buffer, recvfrom (buflen[, Flags])--Receive data and send Er ' s address recvfrom_into (buffer[, nbytes, [, flags])--Receive data and sender's address (into a buffer) Sendall (data[, Flags])--Send All data sent to the remote host, after 3.x can only send byte form, so at the time of transmission is generally to convert bytes send (data[, flags])--send data, may not send all of it is also send data, district Do not be sent by send incomplete, randomly sent, two sendall sent the full sendto (data[, flags], addr)--send data to a given address UDP-based transmission of the Setblocki Ng (0 | 1)--set or clear the blocking I/O flag is set to block mode 0 for blocking, 1 for non-blocking setsockopt (level, optname, value)--Set sock Et options set some sockets for Eucalyptus settimeout (None | float)--set or clear the timeout setting time out market shutdown (how)--shut do WN traffic in one or both directions If_nameindex () – Return all network interface indices and names If_nametoi Ndex (name)--return the corresponding interface index If_indextoname (index)--return the corresponding interface name [*] not available on all platforms!
Two. Simple chat robot
If a data is sent, the server will reply to a data + Hello
1 #-*-Coding:utf-8-*-2 # Zhou 3 # 2017/7/3 4 5 Import Socket 6 # Create a server object 7 Server_obj = Socket.socket () 8 # Tied Fix the Port 9 server_obj.bind (("127.0.0.1", 8888,)) 10 # Set the listening queue length to 5, and reject the connection when it is greater than 5 Server_obj.listen (5) while true:14< c1/># waits for the client connection to be blocked, conn, address = Server_obj.accept () # Send a welcome message to Conn.sendall (bytes (" Welcome to the simple chat room: ", encoding= ' Utf-8 ')) while true:19 # receives the message to the opposite side and adds you back to the opposite message. ret = str (CONN.RECV ( (1024x768), encoding= ' Utf-8 ') if ret = = ' Q ': # If the opposite is sent for Q then exit the break24 conn.sendall (ret + "Hello" , encoding= ' Utf-8 '))
Server
#-*-Coding:utf-8-*-# zhou# 2017/7/3import socketclient = Socket.socket () client.connect (("127.0.0.1", 8888,)) # Accept the Welcome message and print ret = str (CLIENT.RECV (1024x768), encoding= ' Utf-8 ') print (ret) while True: message = input ("Please enter what you want to send:") Client.sendall (bytes (message, encoding= ' Utf-8 ')) if message = = ' Q ': Breakret = str (CLIENT.RECV (1024x768), encoding= ' Utf-8 ') Print (ret)
Client
Three. Simple FTP upload
Implemented to upload a picture to the server side
1 #-*-Coding:utf-8-*-2 # Zhou 3 # 2017/7/2 4 5 Import socket 6 7 server = Socket.socket () 8 Server.bind (("127. 0.0.1 ", 9998,)) # bound IP 9 server.listen (5) while True:12 conn, address = Server.accept () # First receive file size after connection file_size = Int (str (CONN.RECV (1024x768), encoding= ' Utf-8 ')) # ( conn.sendall ("1001") to solve the problem of sticky packets , encoding= ' utf-8 ') * * The file size accepted is has_size = 019 num = # # of files received after the file is connected to open ("new.jpg", ' WB ') while true:23 num + = 124 if file_size = = has_size:25 break26 data = CONN.RECV (1024) 27< C18/>f.write (data) has_size + = len (data) f.close () # Close File
Ftpserver
1 #-*-Coding:utf-8-*-2 # Zhou 3 # 2017/7/2 4 5 6 import OS 7 import Socket 8 9 client = Socket.socket () 10 Client.connect ("127.0.0.1", 9998), 12 # Transfer File size: File_size = Os.stat ("1.jpg"). St_size14 print (file_size) 15 # Send File size 1 6 Client.sendall (bytes (str (file_size), encoding= ' Utf-8 ')) Client.recv (1024x768) # fix Sticky pack Problem 18 # Send file with open ("1.jpg ", ' RB ') ' as f:20 for line ' f:21 client.sendall (line)-Client.close ()
ftpclient
Four. Solving the problem of sticky bag
For a description of the third FTP upload above,
Solve the problem of sticky, when we upload a file, the first upload his size, when we upload the size to write a sentence to accept, and the server side after accepting the file size to send us immediately sent a data to confirm, so that we can perfect the data drink size split open.