Python code implementation for small chat rooms and python code for chat rooms

Source: Internet
Author: User

Python code implementation for small chat rooms and python code for chat rooms

Python can also be used in Java chat rooms. It can also be more elegant. You will certainly like Python Socket with so many streams missing.
As for the knowledge point-related content, I will not talk about it here.

UDP

Server

# Coding: UTF-8 # _ author _ = 'Mark sinoberg '# _ date _ = '2017/7' # _ Desc _ = create a simple socket listener request import socketHOST = '2017. 168.59.255 'port = 9998 s = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) s. bind ('', PORT) print 'socket started! 'While True: data, addr = s. recvfrom (1024) print addr, str (':') + data

Client

# Coding: UTF-8 # _ author _ = 'Mark sinoberg '# _ date _ = '2017/7' # _ Desc _ = simple implementation of the socket Client import socketPORT = 9998 HOST = '2017. 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 ()

Is it easy. Note that the second parameter of socket is SOCK_DGRAM. This is different from the TCP SOCK_STREAM method.

TCP Mode

Server

# Coding: UTF-8 # _ author _ = 'Mark sinoberg '# _ date _ = '2017/7' # _ Desc _ = simple implementation of tcpsocket from socket import * from time import ctimeHOST = ''PORT = 9999 BUFFERSIZE = 1024 ADDRESS = (HOST, PORT) s = socket (AF_INET, SOCK_STREAM) s. bind (ADDRESS) s. listen (5) while True: print 'Waiting for clients cennect! 'Tcpclient, addr = s. accept () print 'connectedby', addr while True: try: data = tcpclient. recv (BUFFERSIZE) failed t Exception, e: print e. message tcpclient. close () break if not data: print "No Data already ed! "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 _ = '2017/7' # _ Desc _ = simple tcp socket client implementation from socket import * class TcpClient: # HOST = 'localhost' PORT = 9999 HOST = '2017. 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 'pleas E 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 mode Demonstration Result(Enable the server first)

Server

D: \ Software \ Python2 \ python.exe E:/Code/Python/MyTestSet/sockettest/SimpleTCPServer. py
Waiting for clients cennect!
Connected By ('192. 168.59.225 ', 192)
('192. 168.59.225 ', 192) Says: Thu Jul 07 16:01:10 63095 Hello World
('192. 168.59.225 ', 192) Says: Thu Jul 07 16:01:15 63095 haode
No Data already ed!
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 07 16:01:10 2016] Hello World
>>>
Please input some words!
>>>
>>> Haode
Hi, you send me: [Thu Jul 07 16:01:15 2016] haode
>>> Quit

Process finished with exit code 0

Summary

It is really easy to simply use TCP or UDP. However, to make better use of these two protocols, we need to make a good design.

Here I want to emphasize that pay attention to the parameter specified when tcp and udp are used to create a socket.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.