Python Basics-day33__python

Source: Internet
Author: User
#TCP, server-side code, import socket ' ' first imports Socke module ' SK = Socket.socket () ' Create a Socket object, Def __init__ (self, family=af_inet , Type=sock_stream, Proto=0, Fileno=none): The default parameter means, Family=af_inet, the protocol family default IPv4, Type=sock_stream, the default is TCP, the latter two are not involved in the " ' Sk.bind ((' 127.0.0.1 ', 9999)] ' We are bound to our local address via socket this service, (Ip,port) as a server, you need to set up your own information so that others know how to connect you, Def bind (self, Address): # Real signature unknown;
Restored from __doc__ Bind the socket to a. ' Sk.listen () #等待别人来连接你 ' Def listen (self, backlog=none): # Real signature unknown;  
        Restored from __doc__ Enable a server to accept connections.

Backlog=none, not currently involved, but it should be about the number of connections? ' conn,addr = sk.accept () ' This function will stagnate here, wait for others to connect, and if the connection succeeds, it will return the connection information, Def accept (self): Accept ()-> (socket  object, address info) a incoming connection.
        Return a new socket representing the connection, and the address of the client. For IP sockets,The address info is a pair (hostaddr, port). Waiting to come to the connection, returns a (new socket, address information) on behalf of the new connection, ' Print (addr) conn.send (' Hello '. Encode (' Utf-8 ')) ' sends data in string form, returns the number of bytes sent, Def send (sel F, data, Flags=none): # Real signature unknown;  Restored from __doc__ send (data[, flags])-> The count send a data string to the socket.  For the optional Flags argument, this is the Unix manual. Return the number of bytes sent;
This could be less than Len (data) If the network is busy.  ' ret = CONN.RECV (1024) #1024 to accept 1024 bytes ' def recv (self, buffersize, flags=none): # Real signature unknown; Restored from __doc__ recv (buffersize[, flags])-> data ' Print (Ret.decode (' Utf-8 ')) Conn.close () # Hang Power The words sk.close () # Off the phone ' here I's two sockets, respectively, 1. The socket used by the server for listening, 2. The newly established connection sockets require us to turn def close (self) on: # this Functio n should not reference any globals.
        Issue #808164. self._closed = True If self._io_refs <= 0: #一个套接字可能涉及多个远方多个数据的收发, soThere are other connections to be judged, self._real_close () " 



#TCP, client-side code
import socket

SK = Socket.socket ()
sk.connect (' 127.0.0.1 ', 9999)
'
    def Connect (self, address): # Real signature unknown; Restored from __doc__
        Connect (address) '

ret = SK.RECV (1024)
print (Ret.decode (' Utf-8 ')
) Sk.send (' You are also good '. Encode (' Utf-8 '))

Sk.close ()

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.