udp, socketserver module usage

Exception Handling

Program error has occurred unpredictable in operation

And the error is no corresponding processing mechanism, it will be shown in the form of abnormal

The entire program can not run

Unusual species

1 NameError name wrong

2 SyntaxError Syntax error

3 KeyError key does not exist

4 ValueError value error

5 IndexError index error

6 Exception / BaseException can accept all wrong

How to avoid exception handling you think the bug may occur above the code block try this: pay attention to try inside a block of code as possible

        try: 
            possible error code 
        except the type of error as e: # the error message assigned to the variable e 
            processing mechanism after error 
try: 
    name 
    L = "asdasd" 
    L [12,312,313] 
except Exception: 
    Print ( "asdasd") 
the else : 
    Print ( "asdasd") 
a finally: 
    Print ( "assdasd")

This is the most complex structure of the abnormal

udp communication

Also known as datagram protocol (own header)

No two-way channel, compared with the tcp, udp is to send text messages (just sent you, regardless of whether you receive), tcp is to call (need we need only to dial phone communication)

Code format, after the different tcp

Server

socket Import 

# Create a socket object 
Server = socket.socket (of the type = socket.SOCK_DGRAM) 
# need to bind to port 
server.bind (( "127.0.01", 8181)) 

# place directly and receive messages, do not need listen and accept the 

DATE, server.recvfrom address = (1024) 
Print (DATE, address) 
server.sendto (b'adassd ', address)

Client

Socket Import 
# Create a socket object 
Client = socket.socket (type = socket.SOCK_DGRAM) 
# set an IP address and a port number variable 
address = ( "127.0.0.1", 8181) 
# message sent by sendto and requires two parameters a message is also sent to a address, except that the tcp, tcp with only one parameter needs to send 
client.sendto (b'asdasda ', address) 
# after receiving the message has two parameters the first is a message content, there is a port tcp ip only one parameter, the received information is 
DATE, client.recvfrom address = (1024) 
Print (DATE, address)

Based on udp we can write a simple version of the QQ

Server

import socket
​
server = socket.socket(type=socket.SOCK_DGRAM)
server.bind(("127.0.0.1",8787))
while True:
​
    date,address = server.recvfrom(1024)
    print(date.decode("utf-8"),address)
​
    msg = input("请输入内容").strip().encode("utf-8")
​
    server.sendto(msg,address)

Client

import socket
​
client = socket.socket(type=socket.SOCK_DGRAM)
address = ("127.0.0.1",8787)
​
while True:
    msg = input("请输入内容").strip().encode("utf-8")
    client.sendto(msg,address)
​
    date,addre = client.recvfrom(1024)
    print(date.decode("utf-8"),addre)

udp of the four characteristics

1 allows the hair empty

2 non-stick package

3 protocol server is disconnected, the client will not complain

4 support concurrent

socketserver

Allows, tcp to udp as concurrent programming

Server can simultaneously communicate with multiple clients

Server

SocketServer Import 
# create a class that inherits of SocketServer.BaseRequestHandler 
class MyServer (of SocketServer.BaseRequestHandler): 
    # define a method handler written inside the statement is sent, and receive statements 
    DEF handle (Self): 
        the while True: 
            DATE = self.request.recv (1024) 
            Print (self.client_address) 
            Print (date.decode ( "UTF-8")) 
            self.request.send (date.upper ()) 
IF __name__ == '__main__': 
    # create objects, ports, and class that we created, the object passed to 
    Server socketserver.ThreadingTCPServer = (( "127.0.0.1", 8888), MyServer) 
    # call serve_forever method 
    server.serve_forever ()
    

Client 1

Can multiple clients, and server interaction

import socket
​
client = socket.socket()
client.connect(("127.0.0.1",8888))
​
while True:
    client.send(b'asdad')
    date = client.recv(1024)
    print(date)

  

Client 2

import socket
client = socket.socket()
client.connect(("127.0.0.1",8888))
while True:
    client.send(b'asdad')
    date = client.recv(1024)
    print(date)

  

 

Guess you like

Origin www.cnblogs.com/cherish937426/p/11323163.html