Chinaunix首页 | 论坛 | 博客
  • 博客访问: 276365
  • 博文数量: 103
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-02 16:15
文章分类

全部博文(103)

文章存档

2014年(8)

2013年(95)

我的朋友

分类: Python/Ruby

2013-09-23 17:24:28

服务器端socket

import SocketServer

import datetime

 

#host_status = {}

 

class MonitorHandler(SocketServer.BaseRequestHandler):

    def handle(self):

        recv_data=self.request.recv(1024)

        print recv_data

 

if __name__ == "__main__":

    host = ''

    port = 18000

 

    server = SocketServer.ThreadingTCPServer((host,port),MonitorHandler)

    server.serve_forever()

 

客户端:

import socket

 

host='192.168.1.130'

port=18000

 

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.connect((host,port))

s.send('up')

s.close()

可用的方法:

fileno() 

返回一个整数描述符  表示服务器正在监听

通常用于传递给select.select()

handle_request()

serve_forever(poll_interval=0.5)

持续的处理请求,除非明确的遇到shutdown()函数,否则持续的请求

poll_interval=0.5   没多少秒轮询关闭

address_family

server_address

服务器正在监听的客户端的地址

shutdown()

address_family

RequestHandlerClass

server_address

socket

allow_reuse_address

request_queue_size

socket_type

timeout

finish_request()

get_request()

BaseServer.handle_error(request, client_address)

BaseServer.handle_timeout()

BaseServer.process_request(request, client_address)

BaseServer.server_activate()

BaseServer.server_bind()

BaseServer.verify_request(request, client_address)

 

------------------------------------------------------------------------------------------------

进一步优化server端,使其能打印连接信息

import SocketServer

import datetime

 

host_status = {}

 

class MonitorHandler(SocketServer.BaseRequestHandler):

    def handle(self):

        #receive information from client

        recv_data=self.request.recv(1024).strip()

        #pritn client address

        c_addr = self.client_address

        host_status[c_addr[0]] = [(datetime.datetime.now(),recv_data)]

        print "Connected from %s:%s %s"%(c_addr,datetime.datetime.now(),recv_data)

 

#        for t,m in host_status.items():

#            print t,m

 

if __name__ == "__main__":

    host = ''

    port = 18000

 

    server = SocketServer.ThreadingTCPServer((host,port),MonitorHandler)

server.serve_forever()

 

显示结果:

root@ubuntu:/opt/oldboy/jk# python ser.py

Connected from ('192.168.1.130', 34326):2013-09-23 16:24:09.523403 up

Connected from ('192.168.1.101', 36491):2013-09-23 16:24:13.121068 up

---------------------------------------------------------------

客户端每30秒向服务器端发送一次存活信息:

import SocketServer

import datetime

 

host_status = {}

 

class MonitorHandler(SocketServer.BaseRequestHandler):

    def handle(self):

        while 1:

            #receive information from client

            recv_data=self.request.recv(1024).strip()

            if not recv_data:break

            #pritn client address

            c_addr = self.client_address

            host_status[c_addr[0]] = [(datetime.datetime.now(),recv_data)]

            print "Connected from %s:%s %s"%(c_addr,datetime.datetime.now(),recv_data)

 

#        for t,m in host_status.items():

#            print t,m

 

if __name__ == "__main__":

    host = ''

    port = 18000

 

    server = SocketServer.ThreadingTCPServer((host,port),MonitorHandler)

    server.serve_forever()

--------------------

import socket

import time

 

host='192.168.1.130'

port=18000

 

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.connect((host,port))

while True:

    try:

        s.send('up')

        time.sleep(10)

    except:

        s.close()

s.close()

 

 

 

阅读(1587) | 评论(0) | 转发(0) |
0

上一篇:14-python-snmp

下一篇:16-snmp-OID

给主人留下些什么吧!~~