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.