Python Simple multithreaded link implementation Code _python

Source: Internet
Author: User
Tags add time datetime readline socket

Service side:

#!/usr/bin/env
Import Socketserver
class Mymonitorhandler (Socketserver.baserequesthandler):
 def Handle (self):
  self.data=self.request.recv (1024). Strip ()
  print "from%s:%s"% (self.client_address, Self.data)
if __name__== "__main__":
 host,port= ' 0.0.0.0 ', 18000
 server= Socketserver.threadingtcpserver ((host,port), Mymonitorhandler)
 server.serve_forever ()
 server.close ()

Client:

#!/usr/bin/env python
import socket
host,port= "10.23.30.191", 18000
s=socket.socket (socket.af_inet , socket. SOCK_STREAM)
S.connect ((host,port))
s.send ("Up")

Server-side Run results:
From (' 10.23.30.61 ', 45809): Up
From (' 10.23.30.61 ', 45810): Up
From (' 10.23.30.61 ', 45811): Up
From (' 10.23.30.61 ', 45812): Up
From (' 10.23.30.61 ', 45813): Up
From (' 10.23.30.61 ', 45814): Up
From (' 10.23.30.61 ', 45815): Up

Python Simple multithreaded links (ii)

One. Simple multithreading (server-side added time tag)
1.1 Service side

#!/usr/bin/env
import datetime
import socketserver
Classmymonitorhandler ( Socketserver.baserequesthandler):
    def handle (self):
       self.data=self.request.recv (1024). Strip ()
        print ' From%s:%s:  %s '% (Self.client_address,datetime.datetime.now (), self.data)
if__name__== "__main__":
    Host,port= ' 0.0.0.0 ', 18000
   server=socketserver.threadingtcpserver ((host,port), Mymonitorhandler)
    Server.serve_forever ()
    server.close ()

1.2 Client:

#!/usr/bin/envpython

Import Socket

host,port= "192.168.1.103", 18000
s=socket.socket (socket.af_ Inet,socket. SOCK_STREAM)
S.connect ((host,port))
s.send ("Up")

Results:
From (' 192.168.1.104 ', 58032): 2013-12-24 06:47:03.620356:up
From (' 192.168.1.104 ', 58033): 2013-12-24 06:47:05.464851:up
From (' 192.168.1.104 ', 58034): 2013-12-24 06:47:06.273092:up

Two. Server Side Add Time label (judge 30 seconds update Send)

2.1 First need to have the dictionary to store the client's information
Define 1 empty dictionaries first

#!/usr/bin/env
importdatetime
importsocketserver
host_status={}

f=open (' Client.txt ')
while 1:
    line=f.readline (). Split ()
    if Len (line) ==0:break
    host_status[line[0]]= []
f.close ()

Classmymonitorhandler (Socketserver.baserequesthandler):
    def handle (self):
       self.data=self.request.recv (1024). Strip ()
        if self.client_address[0] Inhost_status.keys ():
           Host_status[self.client_address[0]]. Append ((Datetime.datetime.now (), self.data))
            print "from%s:%s  :%s"% (self.client_address, Datetime.datetime.now (), Self.data)
        else:
            print "Sorry,ip%sis ' t in the Monitor list!"%self.client_address[ 0]
        for t,m in Host_status.items ():
            print t,m

if__name__== "__main__":
    host,port= ' 0.0.0.0 ', 18000
   Server=socketserver.threadingtcpserver ((host,port), Mymonitorhandler)
    server.serve_forever (
    ) Server.close ()

Explain:

#!/usr/bin/env import datetime import Socketserver host_status={} #定义个空子典用来存储客户端发送的信息和时间 f=open (' client.txt ') #文件的读取 WH Ile 1:line=f.readline (), split () #文件的读取并分段 if Len (line) ==0:break #结尾退出 host_status[line[0]]= [] #初始化字典, set the IP in the file to Dictionary key F.close () Classmymonitorhandler (Socketserver.baserequesthandler): def handle (self): self.data=self.requ EST.RECV (1024). Strip () Ifself.client_address[0] in Host_status.keys (): #如果获取客户端ip在字典的列表的key中 host_status [Self.client_address[0]].append (Datetime.datetime.now (), self.data)) #这儿采用追加的方式 print "from%s:%s:%s"% (self . Client_address,datetime.datetime.now (), self.data) #输出客户端的ip, connection time, sent content Else:print "Sorry,ip%s is ' t i 

n the monitor list! "%self.client_address[0] #不在提示 for T,m inhost_status.items (): #字典输出 printt,m #字典的输出 if__name__== "__main__": host,port= ' 0.0.0.0 ', 18000 server=socketserver.threadingtcpserver ((host,port), MyMonitorHa Ndler) Server.serve_foRever () Server.close () 

This article comes from the "lzt417" blog

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.