python 開乙個執行緒跑socket 客戶端

2021-09-21 07:04:40 字數 3359 閱讀 7070

from socket import *

import time

import threading

import struct

class freguencytcprecv(threading.thread):

def __init__(self, threadname):

threading.thread.__init__(self, name=threadname)

self.host = '192.168.1.10'#伺服器ip

self.port = 8888#埠號

self.data_buffer = ''#初始化buf

def data_received(self, data):

print("這裡面寫解析tcp資料報程式")

self.data_buffer = data

headerlengthsize = 2

headerjumpsize = 32

freguencykhzsize = 4

freguencyindexsize = 2

earfcnsize = 4

picsize = 2

rssidbm100size = 2

rsrpdbm100size = 2

rsrqdbm100size = 2

cinrdbm100size = 2

antennadexsize = 2

buffersize = headerlengthsize + headerjumpsize + freguencykhzsize + freguencyindexsize + earfcnsize + picsize + rssidbm100size + rsrpdbm100size + rsrqdbm100size + cinrdbm100size + antennadexsize

# #print ("buffersize=" )

# #print(self.data_buffer.encode('hex'))

#print "len(self.data_buffer)"

#print len(self.data_buffer)

while true:

if len(self.data_buffer) < buffersize:

return

##headerlength is tuple

headerlength = struct.unpack('#print 'headerlength:%d ' %headerlength

# take the first element of a tuple

#print headerlength[0]

#print int('20', base=16)

if headerlength[0] == int('20',base=16):

freguencykhz, freguencyindex, earfcn, pic, rssidbm100, rsrpdbm100, rsrqdbm100, cinrdbm100, antennadex = struct.unpack('finallyfreguencykhz = freguencykhz/1000

finallyfreguencyindex = freguencyindex

finallyearfcn = earfcn

finallypic = pic

finallyrssidbm100 = rssidbm100/100.0

finallyrsrpdbm100 = rsrpdbm100/100.0

finallyrsrqdbm100 = rsrqdbm100/100.0

finallycinrdbm100 = cinrdbm100/100.0

finallyantennadex = antennadex

print('freguencykhz:%d ,freguencyindex:%d ,earfcn:%d, pic:%d, rssidbm100:%.2f, rsrpdbm100:%.2f, rsrqdbm100:%.2f, cinrdbm100:%.2f, antennadex:%d' %(finallyfreguencykhz, finallyfreguencyindex, finallyearfcn, finallypic, finallyrssidbm100, finallyrsrpdbm100, finallyrsrqdbm100, finallycinrdbm100, finallyantennadex))

return

def run(self):#程序執行start就會直接執行run函式

global tcpenable #global 關鍵字全域性變數

while true:

print("tcpenable= %s" %tcpenable)

if tcpenable:

try:

print('tcp client socket receive buf')

pkt = tcpclisock.recv(1024)

print pkt.encode('hex')#例子:以16進製制列印

# #pkt_hex = pkt.encode('hex')#例子:將包轉換成16進製制

# #print pkt_hex

self.data_received(pkt)#進入解包程式

except exception, errorcode:

print("reconn socket error :%s" % errorcode)

tcpclisock.close()

tcpenable = false

else:

tcpclisock = socket(af_inet, sock_stream)#建立乙個socket

try:

tcpclisock.connect((self.host, self.port))#連線socket伺服器的ip和埠號

print('we are reconnect in recv')

tcpenable = true

except exception, errorcode:

print('connect faild')

if __name__ == '__main__':

tcpenable = false

m = freguencytcprecv('freguencytcprecv')

m.setdaemon(true)#新增這個避免孤兒程序,同時在寫demo的時候主程序要存在。寫了這個主程序結束會結束該程序

m.start()

while true:

print('main thread')

time.sleep(1)

開乙個技術部落格

開乙個技術部落格,是我想了比較久的一件事情了。剛剛開學一直忙著,就拖啊拖的。前幾天看到 unix center 這個 感覺我應該為宣傳這個 做個什麼 就促使我早點把開技術部落格的想法實施。今天下午終於抓到機會,就趕緊寫兩句。做為乙個計算機系的碩士研究生,在學好專業課程的同學,應當時刻關注這個技術的世...

第乙個MapreducerDriver跑起來

注 由hadoop權威指南開始的 mapreducer的用途是資料的儲存和分析,就像lucene一樣要想有個完整的可以執行起來的搜尋引擎肯定要構建索引,然後根據client端的需求進行資料分析一樣。override public void map longwritable key,text valu...

跑乙個 OpenVINO 的 demo

上週在公司聽了 intel 的代表來介紹他們的 ai 晶元,spring hill nnp i 1000 關於這個晶元的資料,具體可以看看這個下面的 ppt。因為 gpu 資源太緊缺了,而線上部署的模型,考慮到算力和延時等因素,都不會太複雜,所以實際場景下做推斷,還是大量的 cpu 為主的。鑑於 i...