Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A three-step introduction to linux Network programming

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report