Serial 52-SOCKET Python programming examples

A, UDP programming

1. Client Client: the party initiating access.

2. server

3.server segment Programming

(1) establish socket, socket is responsible for a particular instance of communication

(2) binding to create a socket assigned a fixed IP address and port

(3) to send content to accept each other

(4) sent back to each other, this step must be non-step

4.Client side programming

(1) establishing communications socket

(2) the server sends the content to the specified

(3) accept the feedback given content server

5. simulate what the process

(1) We first establish the function of a server

 

# Server Case

import socket


# Function simulation server

def serverFunc():

    # 1. Establish socket

    # Socket.AF_INET: using IPV4 protocol suite

    # Socket.SOCK_DGRAM:通信serviceable to and on UDP 

    sock = socket.socket, (socket.AF_INET, socket.SOCK_DGRAM),

    # 2. Bind ip and port


    # 127.0.01: ip address represents the machine itself

    # 7852: randomly assigned port numbers

    # Address is a tuple type, (ip.port) 

    addr = ( " 127.0.0.1 " , 7852 )

    sock.bind(addr)


    # Accept each other messages

    # Standby mode for the dead, etc., there is no other possibility

    # Recvfrom return value is a tuple received before a data indicating, after an address indicating

    # Meaning of the parameters is the buffer size

    #rst = sock.recvfrom(500)

    data,addr = sock.recvfrom(500)


    print(data)

    print(type(data))


    # Data bytes are sent over format, you must be to get the str format content by decoding

    # Decode default parameters are utf8 

    text = data.decode ()

    print(type(text))

    print(text)


    # Give each other feedback messages 

    rsp = " the I AM now Very Hungry "


    # Transmitted data bytes to be programmed into a format

    # Default UTF8 

    Data = rsp.encode ()

    sock.sendto(data,addr)

   

if __name__ == "__main__":

    print("Start Server")

    serverFunc()

print("End Server")

 

 

(2) function and then create a client

 

import socket

def clientFunc():

    sock = socket.socket, (socket.AF_INET, socket.SOCK_DGRAM),

    text = "you are very cool"


    # Data transmission format must bytes 

    Data = text.encode ()

    # Send 

    sock.sendto (Data, ( " 127.0.0.1 " , 7852 ))

    data,addr =sock.recvfrom(200)


    data = data.decode()

    print(text)


if __name__ == "__main__":

    print("Start Client")

    clientFunc ()

print("End Client")

 

 

(3) Let's start the server-side function

Appears as an image, it is the creation of a sock, then bind itself to 127.0.0.1:7852, run to the sock.recvfrom (500) died here and so client message

 


(4) Restart the client function

transmitting the encoded text follows after to the server (127.0.0.1:7852), returns the server you are cool successfully received phrase.

 

 

(5) We look at server function there

Received a string of bytes format and print it out, and then decode print it out, you are cool and finally returned to the client.

 

 

(6) There is no way to place: Because we do on a computer test, so the equivalent of himself, as tested on two computers.

Second, the source

D33_1_ServerProgramme.py

D33_2_ClientProgramme.py

https://github.com/ruigege66/Python_learning/blob/master/D33_1_ServerProgramme.py

https://github.com/ruigege66/Python_learning/blob/master/D33_2_ClientProgramme.py

2.CSDN: https: //blog.csdn.net/weixin_44630050 (Xi Jun Jun Moods do not know - Rui)

3. Park blog: https: //www.cnblogs.com/ruigege0000/

4. Welcomes the focus on micro-channel public number: Fourier transform public personal number, only for learning exchanges, backstage reply "gifts" to get big data learning materials

 

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/11894654.html