03python network programming client.

Time:2021-9-27
#This is the client side. I will write several scripts on the single threaded, multi-threaded and asynchronous server side of the service# Define a client and # import the module of receiving terminal parameters, random numbers, sockets and tools written by yourself. import argparse, random, socket, zen_ Utils# defines the client function, and the input parameter is the IP address port. Def client (address, cause_error = false): # define a socket. Socket = socket. Socket (socket. Af_inet, socket. Sock_stream) # connect the server. Sock. Connect (address) # convert the dictionary to a list, aphorisms = list (zen_utils. Aphorisms) # if true, send the terminator. Then it's over. if cause_ Error: sock.sendall (aphorisms [0] [: - 1]) return # if it is not true, send three questions for aphorism in random. Sample (aphorisms, 3): # send the questions, sock.sendall (aphorism) # and then print the answers to the received questions. Print (aphorism, zen_utils. Recv_until (sock, B '.')) # close the socket. sock.close()if __ name__ == '__ main__':     parser = argparse.ArgumentParser(description='Example client')    parser.add_ argument('host', help='IP or hostname')    parser.add_ argument('-e', action='store_true', help='cause an error')    parser.add_ argument('-p', metavar='port', type=int, default=1060,                        help='TCP port (default 1060)')    args = parser.parse_ args()    address = (args.host, args.p)    client(address, args.e)

Recommended Today

Monkey patch monkey patch programming method and its application in Ruby

What ismonkey patch (Monkey Patch)? In a dynamic language, functions are added and changed without modifying the source code. Purpose of using monkey patch:1. Additional function2. Function change3. Fix program errors4. Add a hook to execute some other processing while executing a method, such as printing logs, realizing AOP, etc,5. Cache, when the amount of […]