serverName = '127.0.0.1'# server name serverPort = 12000# server port
clientSocket = socket(AF_INET, SOCK_DGRAM) # create a socket object, AF_INET is the address family for IPv4, SOCK_DGRAM is the socket type for UDP
message = input('Input lowercase sentence:') # input the message to send to the server clientSocket.sendto(message.encode(), (serverName, serverPort)) #convert the message from the string type to byte type modifiedMessage, serverAddress = clientSocket.recvfrom(2048) #receive the message from the server,modifiedMessage is the message received from the server, serverAddress is the address of the server print(modifiedMessage.decode()) #convert the message from the byte type to string type clientSocket.close() #close the socket
UDPServer.py
1 2 3 4 5 6 7 8 9 10 11
from socket import * # import socket module serverPort = 12000# server port
serverSocket = socket(AF_INET, SOCK_DGRAM) # create a socket object, AF_INET is the address family for IPv4, SOCK_DGRAM is the socket type for UDP serverSocket.bind(('', serverPort)) # bind the socket to the port print("The server is ready to receive") # print the message to the user whileTrue: # loop to receive the message from the client message, clientAddress = serverSocket.recvfrom(2048) # receive the message from the client, message is the message received from the client, clientAddress is the address of the client modifiedMessage = message.upper() # convert the message to uppercase serverSocket.sendto(modifiedMessage, clientAddress) # send the message to the client serverSocket.close() # close the socket
(base) gujunxiang@gujunxiangdeMacBook-Pro ~ % ssh [email protected] @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the ED25519 key sent by the remote host is SHA256:zbyGP1aHgYZNxh7Die+ubzIou8j44UMoUlkdp671wOE. Please contact your system administrator. Add correct host key in /Users/gujunxiang/.ssh/known_hosts to get rid of this message. Offending ECDSA key in /Users/gujunxiang/.ssh/known_hosts:9 Host key for 106.14.158.150 has changed and you have requested strict checking. Host key verification failed.