Chat Room Python Code implementation

Source: Internet
Author: User
Python can do the same with Java-style chat rooms. And can do more elegant. Presumably there are so many different streams of Python sockets that you will love.

As for the content of knowledge points, there is not much to say.

UDP mode

Server-side

# coding:utf-8#  __author__ = ' Mark Sinoberg ' #  __date__ = ' 2016/7/7 ' #  __desc__ = Create a simple socket listener request Import Sockethost = ' 192.168.59.255 ' PORT = 9998s = Socket.socket (socket.af_inet,socket. SOCK_DGRAM) S.bind ((", PORT)) print ' socket started! ' While True:  data,addr = S.recvfrom (1024x768)  print addr,str (': ') +data

Client

# coding:utf-8#  __author__ = ' Mark Sinoberg ' #  __date__ = ' 2016/7/7 ' #  __desc__ = Simple implementation of client for socket import Socketport = 9998HOST = ' 192.168.59.255 ' s = Socket.socket (socket.af_inet,socket. SOCK_DGRAM) words = Raw_input (' Client: ') while words! = ' quit ':  s.sendto (words, (host,port))  words = Raw_input (' Client: ') s.close ()

Isn't it simple? What we need to note is that the second parameter of the socket is sock_dgram. Because this is different from the sock_stream of the TCP method.

TCP mode

Server-side

# coding:utf-8#  __author__ = ' Mark Sinoberg ' #  __date__ = ' 2016/7/7 ' #  __desc__ = simple Tcpsocket implementation from socket Import *from time Import ctimehost = "PORT = 9999BUFFERSIZE = 1024ADDRESS = (host,port) s = socket (Af_inet,sock_stream) s.b IND (ADDRESS) S.listen (5) while True:  print ' Waiting for clients cennect! '  TCPCLIENT,ADDR = s.accept ()  print ' Connected by ', addr while  True:    try:      data = TCPCLIENT.RECV ( buffersize)    except exception,e:      print e.message      tcpclient.close ()      break    if not data:      Print "No Data received!"      Break    senddata = ' hi,you send me:[%s]%s '% (CTime (), Data.encode (' UTF8 '))    tcpclient.send (Senddata.encode (' UTF8 '))    print addr, ' Says: ', CTime (), Data.encode (' UTF8 ') tcpclient.close () S.close ()

Client

# coding:utf-8#  __author__ = ' Mark Sinoberg ' #  __date__ = ' 2016/7/7 ' #  __desc__ = Simple TCP socket client implementation from Socket import *class TcpClient:  # host = ' localhost '  PORT = 9999  host = ' 192.168.59.225 '  buffsiz = 1024
  addr = (host,port)  def __init__ (self):    self.client = socket (af_inet,sock_stream)    Self.client.connect (self. Host,self. PORT) while    True:      senddata = raw_input (' >>> ')      if not senddata: "        print ' please input some Words!\n>>> '        continue      if senddata = = "Quit": Break      Self.client.send (Senddata.encode ( ' UTF8 '))      RecvData = Self.client.recv (self. Buffsiz)      if not recvdata:        break      print recvdata.encode (' UTF8 ') if __name__ = = "__main__":  client = TcpClient ()

TCP Demo Result : (note open server side first)

Server-side

D:\Software\Python2\python.exe e:/code/python/mytestset/sockettest/simpletcpserver.py
Waiting for clients cennect!
Connected by (' 192.168.59.225 ', 63095)
(' 192.168.59.225 ', 63095) Says:thu Jul 16:01:10 Hello World
(' 192.168.59.225 ', 63095) Says:thu Jul 16:01:15 Haode
No Data received!
Waiting for clients cennect!

Client

D:\Software\Python2\python.exe e:/code/python/mytestset/sockettest/simpletcpclient.py
>>>hello World
hi,you send Me:[thu Jul 16:01:10 2016]hello World
>>>
Please input some words!
>>>
>>>haode
hi,you send Me:[thu Jul 16:01:15 2016]haode
>>>quit

Process finished with exit code 0

Summarize

The simple use of TCP or UDP is really easy, but to better use the two protocols, you need to design a good.

What I want to emphasize here is that the parameters that are specified when TCP and UDP create sockets are noted.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

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.