日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

scoket多线程例子

發布時間:2025/3/8 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 scoket多线程例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

大體思路,有n臺mc,要dump出數據,n臺進行對比,看數據是否一致,設計到同時dump的問題,server斷發條指令給這n臺mc,mc同時去dump把結果返回給server端,server端把這些結果進行對比


server段

#!/usr/bin/python

#coding:utf8

import socket

import os

import threading

import time

import sys


def work_1(ip,conn):

? ? ? ? global should_stop

? ? ? ? while True:

? ? ? ? ? ? ? ? '''

? ? ? ? ? ? ? ? if should_stop:

? ? ? ? ? ? ? ? ? ? ? ? print "should_stop"

? ? ? ? ? ? ? ? ? ? ? ? break

? ? ? ? ? ? ? ? '''

? ? ? ? ? ? ? ? time.sleep(10)

? ? ? ? ? ? ? ? conn.send(ip)

? ? ? ? ? ? ? ? client_data=conn.recv(1024)

? ? ? ? ? ? ? ? data1=client_data.split(" ")[0]

? ? ? ? ? ? ? ? try:

? ? ? ? ? ? ? ? ? ? ? ? data2=client_data.split(" ")[1]

? ? ? ? ? ? ? ? except IndexError, e:

? ? ? ? ? ? ? ? ? ? ? ? print "should_stop set"

? ? ? ? ? ? ? ? ? ? ? ? should_stop = True

? ? ? ? ? ? ? ? bb=open('/home/text.txt','a')

? ? ? ? ? ? ? ? bb.write(client_data+'\n')

? ? ? ? ? ? ? ? print time.time(),client_data




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

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

s.bind(('10.13.144.224',9999))

s.listen(10)

s.setblocking(0)

s.settimeout(10)

ip='10.13.144.224'

list_1=[]

should_stop = False


while True:

? ? ? ? if should_stop: break

? ? ? ? try:

? ? ? ? ? ? ? ? conn,addr=s.accept()

? ? ? ? except socket.timeout:

? ? ? ? ? ? ? ? continue

? ? ? ? print "client timeout: ",conn.gettimeout()

? ? ? ? a=0

? ? ? ? aa=threading.Thread(target=work_1,args=(ip,conn))

? ? ? ? list_1.append(aa)

? ? ? ? print list_1

? ? ? ? aa.start()

print "join"

for i in list_1:

? ? ? ? i.join()

print '+++++'

cc=open('/home/text.txt','r')

cd=open('/home/text1.txt','w')

ca=cc.readlines()

for ii in ca:

? ? ? ? if ii == '':

? ? ? ? ? ? ? ? break

? ? ? ? elif ii == '\n':

? ? ? ? ? ? ? ? pass

? ? ? ? else:

? ? ? ? ? ? ? ? cd.write(ii)

cd.close()

cg=open('/home/text1.txt','r')

cf=cg.readlines()

dict_1={}

for iiii in cf:

? ? ? ? print 'ok'

? ? ? ? dict_1[iiii.split(" ")[0]]=iiii.split(" ")[1]

print dict_1

print int(dict_1[dict_1.keys()[0]])-int(dict_1[dict_1.keys()[1]])

cz=open('/home/text.txt','w')

cz.truncate()

cy=open('/home/text1.txt','w')

cy.truncate()

print 'exit'

? ? ? ? ? ?


client斷

#!/usr/bin/python

#coding:utf8

import socket

import time

import telnetlib


def mem_status(ip,port):

? ? ? ? print ip

? ? ? ? print port

? ? ? ? tn=telnetlib.Telnet(ip,port)

? ? ? ? tn.set_debuglevel(1)

? ? ? ? tn.write('dump /data0/logs/b.log'+'\n')

? ? ? ? tn.write('quit'+'\n')

? ? ? ? return 'ok'


def work_lines(file):

? ? ? ? print file

? ? ? ? open_file=open(file,'r')

? ? ? ? print open_file

? ? ? ? file_lines=open_file.readlines()

? ? ? ? return file_lines



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

c.connect(('10.13.144.224',9999))

file='/data0/logs/b.log'

rec=c.recv(1024)

if rec == '10.13.144.224':

? ? ? ? return_value=mem_status('10.67.15.11',22224)

? ? ? ? time.sleep(10)

? ? ? ? if return_value:

? ? ? ? ? ? ? ? a=open('/data0/logs/b.log','r')

? ? ? ? ? ? ? ? file_lines=a.readlines()

? ? ? ? ? ? ? ? over='10.67.15.11'+' '+str(len(file_lines))

? ? ? ? ? ? ? ? print over

? ? ? ? ? ? ? ? #over='10.13.144.224',str(len(file_lines))

? ? ? ? ? ? ? ? c.send(str(over))


轉載于:https://blog.51cto.com/4249964/1557426

總結

以上是生活随笔為你收集整理的scoket多线程例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。