Python network Programming--Modifying the buffer size for socket send and receive

Source: Internet
Author: User

In many cases, the default socket buffer size may not be sufficient. At this point, you can change the default socket buffer size to a more appropriate value.

1. Code

1 #! /usr/bin/env python2 #-*-coding:utf-8-*-3 4 ImportSocket5 6 #set send buffer domain size7Send_buf_size = 40968 #setting the Receive buffer domain size9Recv_buf_size = 4096Ten  One  A defmodify_buff_size (): -     #Create a TCP socket -     #UDP socket--s=socket.socket (socket.af_inet,sock_dgram) theSock =Socket.socket (socket.af_inet, socket. SOCK_STREAM) -  -     #gets the options associated with the current socket -     #socket. sol_socket--the SOCKET option being used +     #socket. so_sndbuf--Send buffer size -Bsize =sock.getsockopt (socket. Sol_socket, SOCKET. SO_SNDBUF) +     #send buffer size before printing changes A     Print("Buffer size [before]:%d"%bsize) at  -     #to set options for a TCP socket association -     #socket. Tcp_nodelay TCP Layer Socket interface options -     #the Tcp_nodelay is marked as true -Sock.setsockopt (socket. SOL_TCP, Socket. Tcp_nodelay, 1) -  in     #SET options for sending buffered domain socket associations - sock.setsockopt ( to socket. Sol_socket, + socket. SO_SNDBUF, - send_buf_size) the  *     #SET options for receiving buffered domain socket associations $ sock.setsockopt (Panax Notoginseng socket. Sol_socket, - socket. SO_RCVBUF, the recv_buf_size) +  A     #gets the send buffer domain after setting theBsize =sock.getsockopt (socket. Sol_socket, SOCKET. SO_SNDBUF) +     Print("Buffer size [after]:%d"%bsize) -  $ if __name__=='__main__': $Modify_buff_size ()

2.setsockopt () and getsockopt ()

1 defSetSockOpt (self, level, option, value):#real signature unknown; restored from __doc__2     """3 setsockopt (level, option, value)4 5 Set a socket option. See the Unix manual for level and option.6 The value argument can either is an integer or a string.7     """8     Pass9 Ten defGetSockOpt (self, level, option, Buffersize=none):#real signature unknown; restored from __doc__ One     """ A getsockopt (level, option[, buffersize]), value -  - Get a socket option. See the Unix manual for level and option. the If A nonzero buffersize argument is given and the return value is a - string of that length; otherwise it was an integer. -     """ -     Pass +Subsequent additions explained

3.af_inet and Sock_stream Explained

1 socket.af_inet--network communication between servers 2 socket. Sock_stream provides an ordered, reliable, bidirectional, and connection-based byte stream, using an out-of-band data transfer mechanism to use TCP for Internet address families. the 3 sock_stream type of socket interface is a full bidirectional byte stream. For a stream class socket interface, it must be in a connected state before receiving or sending data. Use the Connect () call to establish a connection to another set of interfaces, after which the data can be transmitted using Send () and recv (). When the session ends, call Closesocket (). Out-of-band data is received as specified by Send () and recv ().

4. Running Results

1 buffer size [before]: 655362 Buffer size [after]: 4096

Python network Programming--Modifying the buffer size for socket send and receive

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.