day 33 udp protocol, socketserver module

udp protocol

# Basic version 
Sever:

Import socket
(. = Socket Server = socket.socket of the type SOCK_DGRAM )

server.bind (( '127.0.0.1', 8003))
# udp do not establish a connection, direct hair
# do not need to listen
# data = server. recvfrom (1024)
data, addr = server.recvfrom (1024)
#data is tuples, the first parameter is not part of the data, the second parameter is the client address
server.sendto (data.upper (), addr)

client :
Import socket

Client = socket.socket (of the type = socket.SOCK_DGRAM)
# Sent directly 
client.sendto (b'lqz ', (' 127.0.0.1 ', 8003))
Data = client.recvfrom (1024)
Print (Data)


#加入通信循环
server:
import socket
server=socket.socket(type=socket.SOCK_DGRAM)
server.bind(('127.0.0.1', 8003))

while True:
data,addr=server.recvfrom(1024)
server.sendto(data.upper(),addr)

client:
import socket
client=socket.socket(type=socket.SOCK_DGRAM)
while True:
  msg=input('>>:')
    # Sent directly 
client.sendto (msg.encode ( 'UTF-. 8'), ( '127.0.0.1', 8003))
Data = client.recvfrom (1024)


whether the stick package #udp
udp features:
- You can send empty (Datagram Protocol, since the lead)
- client and server can not have a party-line (since no connection is established)
udp protocol does not stick package problems (udp protocol called Datagram Protocol), can send empty, not TCP


socketserver module
by module socketsever to write server-side

#tcp agreement

Client
Import socket
soc=socket.socket()
soc.connect(('127.0.0.1',8009))
while True:
soc.send('xxxx'.encode('utf-8'))
print(soc.recv(1024))

server
import socketserver
# Define a class of their own, must inherit BaseRequestHandler 
class MyTcp (of SocketServer.BaseRequestHandler):
# method must be rewritten handle
DEF handle (Self):
the try:
the while True: # communication cycle
# Print (Self)
# message back to the client
#conn object is the Request
# receiving data
Print (self.client_address)
data = self.request.recv (1024)
Print (data)
IF len (data) == 0:
return
# transmit data
self.request.send (data.upper () )
the except Exception:
Pass
IF the __name__ == '__main__':
# Tcp instantiate a connection object is obtained, Threading means that, as long as to the request, it automatically open thread to handle the connection with the interaction data
# The first argument is the address of the binding, the second parameter pass a class
server socketserver.ThreadingTCPServer = (( '127.0.0.1', 8009), MyTcp)
#'ve been listening
# so understanding: be as long as a request, from a thread (a man-made, do interactive)
server.serve_forever ()
#udp协议
client
import socket
client=socket.socket(type=socket.SOCK_DGRAM)
client.sendto('lqz'.encode('utf-8'),('127.0.0.1', 8009))
# client.sendto('hello'.encode('utf-8'),('127.0.0.1', 8009))
# client.sendto('world'.encode('utf-8'),('127.0.0.1', 8009))
# client.sendto(''.encode('utf-8'),('127.0.0.1', 8009))
data=client.recvfrom(1024)
print(data)

Server
Import SocketServer

# own definition of a class, must inherit BaseRequestHandler
class MyTcp (of SocketServer.BaseRequestHandler):
# must be rewritten handle method
DEF handle (Self):
Print (Self)
# Data
Print (self.request [0])
Print (Self .request [. 1])
Print (type (self.request [. 1]))
self.request [. 1] .sendto ( 'xxxx'.encode (' UTF-. 8 '), self.client_address)
# the try:
# the while True : a communication cycle #
# = self.request.recvfrom Data (1024)
# Print (Data)
# the except Exception:
# Pass


IF the __name__ == '__main__':
# instantiate an object to obtain a connection tcp, Threading means that, as long as to the request, it automatically open thread to handle the connection with interactive data
# The first argument is the address of the binding, the second parameter pass a class
Server socketserver.ThreadingUDPServer = (( '127.0.0.1', 8009), MyTcp)
# been monitoring
server.serve_forever ()
 
 
 

Guess you like

Origin www.cnblogs.com/wwei4332/p/11493968.html