Socket server
1 # -*- coding: utf-8 -*- 2 import socket 3 4 5 def socket_handler(): 6 pass 7 8 def create_socket(): 9 sckt = socket.socket() 10 sckt.bind(("127.0.0.1", 8000)) 11 sckt.listen(10) 12 while True: 13 connection, address = sckt.accept() 14 # connection.send(bytes("Hello", encoding="utf-8")) 15 connection.send("Hello") 16 connection.close() 17 18 def main(): 19 create_socket() 20 21 if __name__ == "__main__": 22 main()
Socket Client
1 # -*- coding: utf-8 -*- 2 import socket 3 4 5 def socket_handler(): 6 pass 7 8 def receive_socket(): 9 sckt = socket.socket() 10 sckt.connect(("127.0.0.1", 8000)) 11 data = sckt.recv(1024) 12 print("Got the data from the socket: {}".format( 13 str(data) 14 )) 15 16 def main(): 17 receive_socket() 18 19 if __name__ == "__main__": 20 main()
WSGI
# -*- coding: utf-8 -*- from wsgiref.simple_server import make_server def server_handler(env, response): response("200 OK", [('Content-Type', 'text/html')]) return '<h1>Hello response in server_handler</h1>' def create_server(server): svr = make_server('127.0.0.1', 8000, server) print("Serving HTTP on port 8000 ...") svr.serve_forever() def main(): create_server(server_handler) if __name__ == "__main__": main()