Application of Python socket C/s structure in chat room implementation _python

Source: Internet
Author: User
Tags stdin

Chat room Application of Python socket C/S structure

Service side:

#!/usr/bin/env python #coding: UTF8 import socket,select def broadcast_data (sock,message): For sockets in Conn_list: If socket!= server_socket and socket!= sock:try:socket.send (message) except:socket.close () conn_ List.remove (socket) If __name__ = = "__main__": Conn_list = [] Recv_buffer = 4096 PORT = 9999 Server_socket = Socke T.socket (socket.af_inet, socket. SOCK_STREAM) server_socket.setsockopt (socket. Sol_socket, SOCKET.
 
 SO_REUSEADDR, 1 #端口重用, optional server_socket.bind ((', PORT) Server_socket.listen () conn_list.append (Server_socket) print ' Chat server started on port ' + str (port) while 1:read_sockets,write_sockets,error_sockets = Select.select (co 
    Nn_list,[],[]) for sock in Read_sockets: #新建连接 if sock = = server_socket:sockfd, addr = Server_socket.accept () Conn_list.append (SOCKFD) print "Client (%s,%s) connected"% addr Broadcast_data (SOCKFD, "[%s:%s] entered Roo m\n "% addr) #进入聊天室 Else:try:datA = SOCK.RECV (Recv_buffer) if Data:broadcast_data (sock, "\ r" + ' < ' + str (sock.getpeername ()) + ' > ' + da TA) except:broadcast_data (sock, "client (%s,%s) is offline"% addr) print "Client (%s,%s) is offline"% a DDR Sock.close () Conn_list.remove (sock) continue Server_socket.close ()

Client:

 #!/usr/bin/env python #coding: UTF8 import socket,select,string,sys def prompt (): sys . Stdout.write (' <You> ') Sys.stdout.flush () if __name__ = = "__main__": if (len (SYS.ARGV) < 3): print ' USAG E:python chat_client.py hostname Port ' sys.exit () HOST = sys.argv[1] Port = Int (sys.argv[2)) s = Socket.socket ( Socket.af_inet, Socket. SOCK_STREAM) S.settimeout (2) try:s.connect (HOST, PORT) Except:print ' Unable to connect ' sys.exit () pri NT ' Connected to remote host. Start sending messages ' prompt () while 1:socket_list = [Sys.stdin, S] read_sockets, Write_sockets, Error_socket s = Select.select (Socket_list, [], []) for sock in read_sockets:if sock = = S:data = SOCK.RECV (4096) if n OT data:print ' \ndisconnected from Chat server ' Sys.exit () else:sys.stdout.write (data) prompt ( ) else:msg = Sys.stdin.readline () s.send (msg) prompt () 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.