Python socket C/S structure of the chat room application implementation, pythonsocket

Source: Internet
Author: User

Python socket C/S structure of the chat room application implementation, pythonsocket

Chat Room applications with Python socket C/S Structure

Server:

#! /Usr/bin/env python # coding: utf8 import socket, selectdef broadcast_data (sock, message): for socket in conn_list: if socket! = Server_socket and socket! = Sock: try: socket. send (message) failed T: socket. close () conn_list.remove (socket) if _ name _ = "_ main _": conn_list = [] recv_buffer = 4096 PORT = 9999 server_socket = socket. socket (socket. AF_INET, socket. SOCK_STREAM) server_socket.setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1) # PORT reuse. Optional server_socket.bind ('', port) server_socket.listen (99) conn_list.append (server_socket) print" Chat server started on PORT "+ str (PORT) while 1: read_sockets, write_sockets, error_sockets = select. select (conn_list, [], []) for sock in read_sockets: # create a connection 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 room \ n" % addr) # enter the chat room else: try: data = sock. recv (recv_buffer) if data: broadcast_data (sock, "\ r" + '<' + str (sock. getpeername () + '>' + data) except: broadcast_data (sock, "Client (% s, % s) is offline" % addr) print "Client (% s, % s) is offline "% addr sock. close () conn_list.remove (sock) continue server_socket.close ()

Client:

#!/usr/bin/env python#coding:utf8import socket,select,string,sys def prompt() : sys.stdout.write('<You> ') sys.stdout.flush() if __name__ == "__main__":  if(len(sys.argv) < 3) :  print 'Usage : 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()  print 'Connected to remote host. Start sending messages' prompt()  while 1:  socket_list = [sys.stdin, s]   read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])   for sock in read_sockets:   if sock == s:    data = sock.recv(4096)    if not 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.