Commit 4974c0ba authored by Alan Marchiori's avatar Alan Marchiori

initial

parents
"""
Basic udp echo/caps server
Alan Marchiori 2019
"""
import logging
import socket
import time
def handle_req(skt, data, addr, log, server_port):
"handle a single request"
# interpret as utf-8 string, uppercase, then convert (encode)
# as raw bytes for the network
log.info("{} -> {}: {}".format(addr, server_port, data))
newdata = data.decode("utf-8").upper().encode()
time.sleep(3) # processing delay
skt.sendto(newdata, addr)
log.info("{} <- {}: {}".format(addr, server_port, newdata))
def main(server_port=('127.0.0.1', 8888)):
log = logging.getLogger()
log.info("Starting server on {}".format(server_port))
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as skt:
skt.bind(server_port) # bind socket to the UDP port
while True:
data, addr = skt.recvfrom(1024) # blocking
if data:
handle_req(skt, data, addr, log, server_port)
else:
log.info("*EOF*, goodbye!")
break
if __name__ == "__main__":
FORMAT = '%(asctime)-15s %(levelname)-6s: %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
main()
"""
Threaded udp echo/caps server
Alan Marchiori 2019
"""
import logging
import socket
import time
import threading
def handle_req(skt, data, addr, server_port, log):
"handle a single request"
# interpret as utf-8 string, uppercase, then convert (encode)
# as raw bytes for the network
log.info("[{}] {} -> {}: {}".format(
threading.current_thread().name, addr, server_port, data))
newdata = data.decode("utf-8").upper().encode()
time.sleep(3) # processing delay
skt.sendto(newdata, addr)
log.info("[{}] {} <- {}: {}".format(
threading.current_thread().name, addr, server_port, newdata))
def main(server_port=('127.0.0.1', 8888)):
log = logging.getLogger()
log.info("[{}] Starting threading server on {}".format(
threading.current_thread().name, server_port))
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as skt:
skt.bind(server_port) # bind socket to the UDP port
while True:
data, addr = skt.recvfrom(1024) # blocking
if data:
threading.Thread(
target=handle_req,
args=(skt, data, addr, server_port, log)).start()
else:
log.info("*EOF*, goodbye!")
break
if __name__ == "__main__":
FORMAT = '%(asctime)-15s %(levelname)-6s: %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
main()
"""
Socketserver udp echo/caps server
Alan Marchiori 2019
"""
import logging
import socketserver # note not socket!
import time
class MyUdpServer(socketserver.BaseRequestHandler):
def handle(self):
log = self.server.log # grab log object we added to server instance
data,skt = self.request # unpack request, for UDP it's the packet(data) and address
log.info("Handling request from {}".format(
self.client_address))
addr = self.client_address
# interpret as utf-8 string, uppercase, then convert (encode)
# as raw bytes for the network
log.info("{} -> {}: {}".format(
addr, self.server.server_address, data))
newdata = data.decode("utf-8").upper().encode()
time.sleep(3) # simulate delay
skt.sendto(newdata, addr)
log.info("{} <- {}: {}".format(
addr, self.server.server_address, newdata))
if __name__ == "__main__":
FORMAT = '%(asctime)-15s %(levelname)-6s: %(message)s'
server_port=('127.0.0.1', 8888)
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
server = socketserver.UDPServer(server_port, MyUdpServer)
server.log = logging.getLogger() # bind a logger to the object
server.serve_forever()
"""
Socketserver udp echo/caps server with threading
Alan Marchiori 2019
"""
import logging
import socketserver # note not socket!
import time
import threading # to get thread name
class ThreadedUDPServer(socketserver.ThreadingMixIn, socketserver.UDPServer):
pass
class MyUdpServer(socketserver.BaseRequestHandler):
def handle(self):
log = self.server.log # grab log object we added to server instance
data,skt = self.request # unpack request, for UDP it's the packet(data) and address
log.info("Handling request from {}".format(
self.client_address))
addr = self.client_address
log.info("[{}] {} -> {}: {}".format(
threading.current_thread().name, addr, server_port, data))
newdata = data.decode("utf-8").upper().encode()
time.sleep(3) # processing delay
skt.sendto(newdata, addr)
log.info("[{}] {} <- {}: {}".format(
threading.current_thread().name, addr, server_port, newdata))
if __name__ == "__main__":
FORMAT = '%(asctime)-15s %(levelname)-6s: %(message)s'
server_port=('127.0.0.1', 8888)
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
server = ThreadedUDPServer(server_port, MyUdpServer)
server.log = logging.getLogger() # bind a logger to the object
server.serve_forever()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment