Directory
Socket
A simple chat robot
Simple FTP Upload
Solving the problem of sticky bag
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
#That's all I know for a while, and then I'm going to keep it if I use the rest.Methods of Socket objects (keyword arguments notallowed): _accept ()--Accept connection, returning new socket FD andClient address bind (addr)--bind the socket to a local address bind a socket socket close ()--Close the socket closes a socket connect (addr)--connect the socket to a remote address connected to the remote host connect_ex (addr)--Connect,returnAn error code instead of an exception dup ()--returnA new socket FD duplicated fromFileno () Fileno ( )--returnunderlying file descriptor Getpeername ()--returnRemote Address [*] GetSockName ()--returnLocal address getsockopt (level, optname[, Buflen])--Get Socket Options gettimeout ()--returnTimeoutorNone Listen ([n])--Start listening forIncoming Connections recv (buflen[, flags])--Receive Data Recv_into (buffer[, nbytes[, flags])--Receive data into buffer, recvfrom (buflen[, flags])--Receive data andSender's AddressRecvfrom_into (buffer[, nbytes, [, flags])--Receive data andSender's address (into a buffer)Sendall (data[, Flags])--Send all data to the remote host, 3. x can only be sent in byte form, so it is usually converted at the time of sending bytes send (data[, flags])--Send data, May notSend all of It also sends data, the difference is sent by send incomplete, randomly sent, two sendall sent the full sendto (data[, flags], addr)--Send data to a given address UDP-based setblocking (0| 1)--setorClear the blocking i/O Flag is set to block mode 0 for blocking, 1 for non-blocking setsockopt (level, optname, value)--Set socket Options settings Some sockets of Eucalyptus settimeout (None| FLOAT)--setorclear the Timeout setting time-out market shutdown (how)--Shut down trafficinchOneorboth directions If_nameindex ()--returnAll network Interface Indices andnames If_nametoindex (name)--returnThe corresponding interface index If_indextoname (index)--returnThe corresponding interface name [*] notAvailable 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 #Zhou3 #2017/7/34 5 ImportSocket6 #Create a server object7Server_obj =Socket.socket ()8 #Bind the Port9Server_obj.bind (("127.0.0.1", 8888, ))Ten #Set the wait queue length for listening to 5, and reject the connection when it is greater than 5 OneServer_obj.listen (5) A - whileTrue: - #waits for the client to accept the connection, blocking the way theConn, address =server_obj.accept () - #send a welcome message -Conn.sendall (Bytes ("Welcome to the simple chat room .", encoding='Utf-8')) - whileTrue: + #receiving the opposite message will add you back to the opposite message and you re-send it back. -ret = str (CONN.RECV (1024x768), encoding='Utf-8') + ifRET = ='Q': A #if the opposite is sent for q then exit at Break -Conn.sendall (bytes (ret +", Hello", encoding='Utf-8'))Server
#-*-coding:utf-8-*-#Zhou#2017/7/3Importsocketclient=socket.socket () Client.connect ("127.0.0.1", 8888, ))#Accept welcome information and printret = str (CLIENT.RECV (1024x768), encoding='Utf-8')Print(ret) whileTrue:message= Input ("Please enter what you want to send:") Client.sendall (bytes (message, encoding='Utf-8')) ifMessage = ='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 #Zhou3 #2017/7/24 5 ImportSocket6 7Server =Socket.socket ()8Server.bind (("127.0.0.1", 9998,))#Binding IP9Server.listen (5)Ten One whileTrue: AConn, address =server.accept () - #receive file size first after connection -file_size = Int (str (CONN.RECV), encoding='Utf-8')) the #to solve the problem of sticky packets -Conn.sendall (Bytes ("1001", encoding='Utf-8')) - #the size of the file that has been accepted -Has_size =0 +num = 1 - #receive files after connection +f = open ("new.jpg",'WB') A whileTrue: atnum + = 1 - ifFile_size = =has_size: - Break -data = CONN.RECV (1024) - f.write (data) -Has_size + =len (data) inF.close ()#Close FileFtpserver
1 #-*-coding:utf-8-*-2 #Zhou3 #2017/7/24 5 6 ImportOS7 ImportSocket8 9Client =Socket.socket ()Ten OneClient.connect (("127.0.0.1", 9998), ) A #Transfer File Size -File_size = Os.stat ("1.jpg"). St_size - Print(file_size) the #Send File Size -Client.sendall (bytes (str (file_size), encoding='Utf-8')) -CLIENT.RECV (1024)#solving Sticky pack problems - #Send File +With open ("1.jpg",'RB') as F: - forLineinchF: + Client.sendall (line) AClient.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.
Python Series 8 socket