Socket of Python Series 8 and socket of python Series

Source: Internet
Author: User
Tags set socket socket connect

Socket of Python Series 8 and socket of python Series
Directory

Socket

Simple chatbot

Simple ftp upload

Solution to the stick package Problem

I. socket module

Socket, commonly known as socket, is actually a combination of IP addresses and ports. Similar to this form (ip, port), where ip represents a host, port represents an application, and we can communicate with another host through socket.

Parsing socket source code is being written in the tarnado series of articles .....

1. Communication Method

Tcp Communication

Udp Communication

Unix-based communication

2. socket Method
# I know so much about it now. If I use other products, I will continue to save Methods of socket objects (keyword arguments not allowed): _ accept () -- accept connection, returning new socket fd and client address bind (addr) -- bind the socket to a local address to bind a socket close () -- close the socket to close a socket connect (addr) -- connect the socket to a remote address to connect to the remote host connect_ex (addr) -- connect, return an error code instead of an exc Eption dup () -- return a new socket fd duplicated from fileno () -- return underlying file descriptor getpeername () -- return remote address [*] getsockname () -- return 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 accepts data recv_into (bu Ffer [, nbytes [, flags]) -- receive data (into a buffer) receives data into the buffer, recvfrom (buflen [, flags]) -- receive data and sender's address recvfrom_into (buffer [, nbytes, [, flags]) -- receive data and sender's address (into a buffer) sendall (data [, flags]) -- send all data to send data to the remote host, 3. x can only be sent in bytes. Therefore, bytes send (data [, flags]) -- send data, and may not send all of it is also sent data, the difference is that sending is incomplete, random, two s Complete sendto (data [, flags], addr) sent by endall -- send data to a given address setblocking (0 | 1) sent data based on udp) -- set or clear whether the blocking I/O flag is set to blocking mode 0 indicates blocking, 1 indicates non-blocking setsockopt (level, optname, value) -- set socket options: settimeout (None | float) of some sockets -- set or clear the timeout setting of the timeout market shutdown (how) -- shut down traffic in one or both directions if_nameindex () -- return all network interface Indices and names if_nametoindex (name) -- return the corresponding interface index if_indextoname (index) -- return the corresponding interface name [*] not available on all platforms!
2. Simple chatbots

If you send a data packet, the server will send it a data record + hello

1 #-*-coding: UTF-8-*-2 # zhou 3 #4 5 import socket 6 # create a server Object 7 server_obj = socket. socket () 8 # bind port 9 server_obj.bind ("127.0.0.1", 8888,) 10 # Set the length of the listener's waiting queue to 5, when the value is greater than 5, the system rejects connection to 11 server_obj.listen (5) 12 13 while True: 14 # Wait for the client to be connected. The blocking method is 15 conn, address = server_obj.accept () 16 # Send Welcome Message 17 conn. sendall (bytes ("welcome to the simple chat room .. ", encoding = 'utf-8') 18 while True: 19 # After receiving the opposite message, you will add the opposite message to the message and resend it back to 20 ret = str (conn. recv (1024), encoding = 'utf-8') 21 if ret = 'q': 22 # exit 23 break24 conn if q is sent from the opposite side. sendall (bytes (ret + ", hello", encoding = 'utf-8 '))
Server
#-*-Coding: UTF-8-*-# zhou # import socketclient = socket. socket () client. connect ("127.0.0.1", 8888,) # accept the welcome information and print ret = str (client. recv (1024), encoding = 'utf-8') print (ret) while True: message = input ("Enter the content you want to send:") client. sendall (bytes (message, encoding = 'utf-8') if message = 'q': break ret = str (client. recv (1024), encoding = 'utf-8') print (ret)
Client

 

3. Simple ftp upload

Upload an image to the server

1 #-*-coding: UTF-8-*-2 # zhou 3 #4 5 import socket 6 7 server = socket. socket () 8 server. bind ("127.0.0.1", 9998,) # bind ip 9 server. listen (5) 10 11 while True: 12 conn, address = server. accept () 13 # the file size 14 file_size = int (str (conn. recv (1024), encoding = 'utf-8') 15 #16 conn used to solve the problem of sticking packets. sendall (bytes ("1001", encoding = 'utf-8 ')) 17 # accepted file size 18 has_size = 019 num = 120 # received file 21 f = open ("new.jpg", 'wb') 22 while True: 23 num ++ = 124 if file_size = has_size: 25 break26 data = conn. recv (1024) 27 f. write (data) 28 has_size + = len (data) 29 f. close () # close a file
Ftpserver

 

1 #-*-coding: UTF-8-*-2 # zhou 3 #4 5 6 import OS 7 import socket 8 9 client = socket. socket () 10 11 client. connect ("127.0.0.1", 9998),) 12 # Transfer File Size 13 file_size = OS. stat ("1.jpg "). st_size14 print (file_size) 15 # size of the sent file 16 client. sendall (bytes (str (file_size), encoding = 'utf-8') 17 client. recv (1024) # solve the problem of sticking packets 18 # send the file 19 with open ("1.jpg", 'rb') as f: 20 for line in f: 21 client. sendall (line) 22 client. close ()
Ftpclient

 

4. Solve the Problem of sticking packets

For the description of the third ftp upload,

Solve the Problem of sticking packets. When we upload a file, we first upload the file size. After uploading the file size, we need to write an acceptable statement, after the server receives the file size, it will immediately send us a piece of data for confirmation, so that we can perfectly split the data.

  

 

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.