Get the App
SLTechnology News&Howtos  ›  Servers  › 

A three-step introduction to linux Network programming

Shulou Source: shulou.com Published: 2022-06-03 00:01:04 09月30日 Update

Background: every language will have an entry-level helloworld

Content: after reading the basic knowledge of network programming, I know that there are also small exercises in network programming.

Overview: Echo,Chat,Proxy

There are special exercises on the Internet. Whether it is UNIX network programming or other books, there will be relevant examples.

Echo,chat,Proxy uniformly reproduces the implementation of Python here, mainly because Python is relatively simple, and I will supplement PHP's swoole later.

Echo:

Import os,sys,platformimport BaseHTTPServerfrom SocketServer import ThreadingMixInimport urllib,urllib2import shutilimport reimport timetry: from cStringIO import StringIOexcept ImportError: from StringIO import StringIOclass SimpleHTTPRequestHandler (BaseHTTPServer.BaseHTTPRequestHandler): def do_GET (self): F = StringIO () f.write (self.path) length = f.tell () f.seek (0) self.response (length) if f: shutil.copyfileobj (fre self.wfile) f.close () def response (self Length): self.send_response (200) self.send_header ("Content-type", "text/html") self.send_header ("Content-Length", str (length)) self.end_headers () class ThreadingServer (ThreadingMixIn,BaseHTTPServer.HTTPServer): passif _ _ name__ = = "_ main__": serveraddr = ("") Int (sys.argv [1]) print "LISTEN Port (% s)"% (int (sys.argv [1])) srvr = ThreadingServer (serveraddr,SimpleHTTPRequestHandler) srvr.serve_forever ()

Chat:

Import socket,selectdef broadcast_data (sock Message): for socket in CONNECTION_LIST: if socket! = server_socket and socket! = sock: try: socket.send (message) except: socket.close () CONNECTION_LIST.remove (socket) if _ _ name__ = ='_ main__': CONNECTION_LIST = [] RECV_BUFFER = 4096 HOST = '0.0.0.0' PORT = 3446 server_socket = socket.socket (socket.AF_INET Socket.SOCK_STREAM) server_socket.setsockopt (socket.SOL_SOCKET,socket.SO_REUSEADDR,1) server_socket.bind ((HOST,PORT)) server_socket.listen (10) CONNECTION_LIST.append (server_socket) print "Chat server started on port {}" + str (PORT) while True: read_sockets,write_sockets,error_sockets = select.select (CONNECTION_LIST, [] []) for sock in read_sockets: if sock = = server_socket: sockfd,addr = server_socket.accept () CONNECTION_LIST.append (sockfd) print ("Client (% s) connected"% addr) broadcast_data (sockfd "[% slug% s] entered room\ n"% addr) else: try: data = sock.recv (RECV_BUFFER) if data: broadcast_data (sock, "\ r" +''+ data) except: broadcast_data (sock, "Client (% s) % s) is offline "% addr) print (" Client (% s) is offline "% addr) sock.close () CONNECTION_LIST.remove (sock) continue server_socket.close ()

The chat client is the same.

Chat-Client:

Import socket,select,string,sysdef prompt (): sys.stdout.write (") sys.stdout.flush () if _ _ name__==" _ _ main__ ": if (len (sys.argv)

Tags: Network network programming programming books content basics basics clients clients glitches that is time server knowledge later background freedom language or services. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB Shulou Technology macOS Microsoft