kmisc 2.1.87__tar.gz → 2.1.88__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kmisc
3
- Version: 2.1.87
3
+ Version: 2.1.88
4
4
  Summary: Enginering useful library
5
5
  Home-page: https://github.com/kagepark/kmisc
6
6
  Author: Kage Park
@@ -14,7 +14,6 @@ import zlib
14
14
  import copy
15
15
  import json
16
16
  import random
17
- import struct
18
17
  import string
19
18
  import pickle
20
19
  import random
@@ -31,8 +30,6 @@ import traceback
31
30
  import importlib
32
31
  import subprocess
33
32
  import email.utils
34
- import kmport as kp
35
- from kmport import *
36
33
  from sys import modules
37
34
  from pprint import pprint
38
35
  import fcntl,socket,struct
@@ -48,6 +45,17 @@ from email.mime.base import MIMEBase
48
45
  from distutils.version import LooseVersion
49
46
  from multiprocessing import Process, Queue
50
47
  from email.mime.multipart import MIMEMultipart
48
+ try:
49
+ from kmport import *
50
+ except:
51
+ pip_user_install='--user' if os.environ.get('VIRTUAL_ENV') is None else ''
52
+ os.system('''for i in 1 2 3 4 5; do python3 -m pip install pip --upgrade {} 2>&1 | grep "however version" |grep "is available" >& /dev/null || break; done'''.format(pip_user_install))
53
+ os.system('python3 -m pip install kmport {}'.format(pip_user_install))
54
+ try:
55
+ from kmport import *
56
+ except:
57
+ print('Can not install kmport')
58
+ os._exit(1)
51
59
 
52
60
 
53
61
  global krc_define
@@ -758,8 +766,8 @@ class IP:
758
766
  log=opts.get('log',None)
759
767
  init_time=None
760
768
  if self.IsV4(ip):
761
- if not kp.ping(ip,count=5):
762
- if not kp.ping(ip,count=0,timeout=timeout_sec,keep_good=keep_good,interval=interval,cancel_func=cancel_func,log=log):
769
+ if not ping(ip,count=5):
770
+ if not ping(ip,count=0,timeout=timeout_sec,keep_good=keep_good,interval=interval,cancel_func=cancel_func,log=log):
763
771
  return True
764
772
  return False
765
773
  return default
@@ -811,7 +819,7 @@ class IP:
811
819
  def Ping(self,host='_#_',count=0,interval=1,keep_good=0, timeout=0,lost_mon=False,log=None,stop_func=None,log_format='.',cancel_func=None):
812
820
  #if IsNone(host,chk_val=['_#_'],chk_only=True): host=self.ip
813
821
  host=self.GetIP(host)
814
- return kp.ping(host,count=count,interval=interval,keep_good=keep_good,timeout=timeout,lost_mon=lost_mon,log=log,stop_func=stop_func,log_format=log_format,cancel_func=cancel_func)
822
+ return ping(host,count=count,interval=interval,keep_good=keep_good,timeout=timeout,lost_mon=lost_mon,log=log,stop_func=stop_func,log_format=log_format,cancel_func=cancel_func)
815
823
 
816
824
  def IsJson(src):
817
825
  try:
@@ -2962,7 +2970,7 @@ def find_usb_dev(size=None,max_size=None):
2962
2970
  #Payload Channel : 1 (0x01)
2963
2971
  #Payload Port : 623
2964
2972
 
2965
- def net_send_data(sock,data,key='kg',enc=False,timeout=0,close=False):
2973
+ def net_send_data(sock,data,key='kg',enc=False,timeout=0,close=False,instant=True,log=None):
2966
2974
  # if close=True then just send data and close socket
2967
2975
  # if close=False then it need close socket code
2968
2976
  # ex)
@@ -2986,10 +2994,11 @@ def net_send_data(sock,data,key='kg',enc=False,timeout=0,close=False):
2986
2994
  ndata=struct.pack('>IssI',len(pdata),data_type,enc_tf,nkey)+pdata
2987
2995
  try:
2988
2996
  sock.sendall(ndata)
2989
- if close: sock.close()
2997
+ if close and instant is not False:
2998
+ sock.close()
2990
2999
  return True,'OK'
2991
3000
  except:
2992
- if close:
3001
+ if close and instant is not False:
2993
3002
  if sock: sock.close()
2994
3003
  if timeout > 0:
2995
3004
  #timeout=sock.gettimeout()
@@ -2998,9 +3007,9 @@ def net_send_data(sock,data,key='kg',enc=False,timeout=0,close=False):
2998
3007
  return False,'Sending Socket Timeout'
2999
3008
  return False,'Sending Fail'
3000
3009
 
3001
- def net_receive_data(sock,key='kg',progress=None,retry=0,retry_timeout=30,progress_msg=None):
3010
+ def net_receive_data(sock,key='kg',progress=None,retry=0,retry_timeout=30,progress_msg=None,log=None):
3002
3011
  # decode code here
3003
- def recvall(sock,count,progress=False,progress_msg=None): # Packet
3012
+ def recvall(sock,count,progress=False,progress_msg=None,log=None): # Packet
3004
3013
  buf = b''
3005
3014
  file_size_d=int('{0}'.format(count))
3006
3015
  #if progress: print('\n')
@@ -3009,14 +3018,14 @@ def net_receive_data(sock,key='kg',progress=None,retry=0,retry_timeout=30,progre
3009
3018
  while count:
3010
3019
  if progress:
3011
3020
  if progress_msg:
3012
- StdOut('\r{} [ {} % ]'.format(progress_msg,int((file_size_d-count) / file_size_d * 100)))
3021
+ printf('\r{} [ {} % ]'.format(progress_msg,int((file_size_d-count) / file_size_d * 100)),log=log)
3013
3022
  else:
3014
- StdOut('\rDownloading... [ {} % ]'.format(int((file_size_d-count) / file_size_d * 100)))
3023
+ printf('\rDownloading... [ {} % ]'.format(int((file_size_d-count) / file_size_d * 100)),log=log,dsp='e')
3015
3024
  try:
3016
3025
  newbuf = sock.recv(count)
3017
3026
  except socket.error as e:
3018
3027
  if tn < retry:
3019
- print("[ERROR] timeout value:{} retry: {}/{}\n{}".format(sock.gettimeout(),tn,retry,e))
3028
+ printf("[ERROR] timeout value:{} retry: {}/{}\n{}".format(sock.gettimeout(),tn,retry,e),log=log,dsp='e')
3020
3029
  tn+=1
3021
3030
  TIME().Sleep(1)
3022
3031
  sock.settimeout(retry_timeout)
@@ -3028,9 +3037,9 @@ def net_receive_data(sock,key='kg',progress=None,retry=0,retry_timeout=30,progre
3028
3037
  count -= len(newbuf)
3029
3038
  if progress:
3030
3039
  if progress_msg:
3031
- StdOut('\r{} [ 100 % ]\n'.format(progress_msg))
3040
+ printf('\r{} [ 100 % ]\n'.format(progress_msg),log=log)
3032
3041
  else:
3033
- StdOut('\rDownloading... [ 100 % ]\n')
3042
+ printf('\rDownloading... [ 100 % ]\n',log=log,dsp='e')
3034
3043
  return True,buf
3035
3044
  ok,head=recvall(sock,10)
3036
3045
  if krc(ok,chk=True):
@@ -3039,70 +3048,73 @@ def net_receive_data(sock,key='kg',progress=None,retry=0,retry_timeout=30,progre
3039
3048
  #st_head=struct.unpack('>IssI',Bytes(head))
3040
3049
  st_head=struct.unpack('>IssI',Bytes(head))
3041
3050
  except:
3042
- return [False,'Fail for read header({})'.format(head)]
3051
+ return False,'Fail for read header({})'.format(head)
3043
3052
  if st_head[3] == Bytes2Int(key,encode='utf-8',default='org'):
3044
3053
  # File not found Error log size is 57. So if 57 then ignore progress
3045
3054
  if st_head[0] == 57: progress=False
3046
- ok,data=recvall(sock,st_head[0],progress=progress,progress_msg=progress_msg)
3055
+ ok,data=recvall(sock,st_head[0],progress=progress,progress_msg=progress_msg,log=log)
3047
3056
  if krc(ok,chk=True):
3048
3057
  if st_head[2] == 't':
3049
3058
  # decode code here
3050
3059
  # data=decode(data)
3051
3060
  pass
3052
- if data: return [st_head[1],pickle.loads(data)]
3053
- return [True,None]
3061
+ if data: return st_head[1],pickle.loads(data)
3062
+ return True,None
3054
3063
  else:
3055
- return [ok,data]
3064
+ return ok,data
3056
3065
  else:
3057
- return [False,'Wrong key']
3058
- return ['lost','Connection lost']
3059
- return [ok,head]
3066
+ return False,'Wrong key'
3067
+ return 'lost','Connection lost'
3068
+ return ok,head
3060
3069
 
3061
- def net_put_and_get_data(IP,data,PORT=8805,key='kg',timeout=3,try_num=1,try_wait=[0,5],progress=None,enc=False,upacket=None,SSLC=False,log=True,progress_msg=None):
3070
+ def net_put_and_get_data(IP,data,PORT=8805,key='kg',timeout=3,try_num=1,try_wait=[0,5],progress=None,enc=False,upacket=None,SSLC=False,progress_msg=None,instant=True,log=None):
3062
3071
  sent=False,'Unknown issue'
3063
3072
  for ii in range(0,try_num):
3064
3073
  if upacket: # Update packet function for number of try information ([#/<total #>])
3065
3074
  data=upacket('ntry',[ii+1,try_num],data)
3066
3075
  start_time=TIME().Int()
3067
- sock=net_get_socket(IP,PORT,timeout=timeout,SSLC=SSLC)
3076
+ ok,sock=net_get_socket(IP,PORT,timeout=timeout,SSLC=SSLC,log=log)
3068
3077
  if try_num > 0:
3069
3078
  rtry_wait=(timeout//try_num)+1
3070
3079
  else:
3071
3080
  rtry_wait=try_wait
3072
- sent=False,'Unknown issue'
3081
+ sent=False,'Unknown issue',sock
3073
3082
  try:
3074
- sent=net_send_data(sock,data,key=key,enc=enc)
3083
+ sent=net_send_data(sock,data,key=key,enc=enc,log=log)
3075
3084
  except:
3076
3085
  os.system("""[ -f /tmp/.{0}.{1}.crt ] && rm -f /tmp/.{0}.{1}.crt""".format(IP,PORT))
3077
3086
  if sent[0]:
3078
- nrcd=net_receive_data(sock,key=key,progress=progress,progress_msg=progress_msg)
3079
- return nrcd
3087
+ nrcd=net_receive_data(sock,key=key,progress=progress,progress_msg=progress_msg,log=log)
3088
+ return nrcd,'done',sock
3080
3089
  else:
3081
3090
  if timeout >0:
3082
3091
  if TIME().Int() - start_time >= timeout-1:
3083
- return [False,'Socket Send Timeout']
3092
+ return False,'Socket Send Timeout',sock
3084
3093
  #return [False,'Data protocol version mismatch']
3085
- if sock: sock.close()
3094
+ if sock and instant is True:
3095
+ sock.close()
3096
+ sock=None
3086
3097
  if try_num > 1:
3087
- if log:
3088
- print('try send data ... [{}/{}]'.format(ii+1,try_num))
3098
+ printf('try send data ... [{}/{}]'.format(ii+1,try_num),log=log)
3089
3099
  TIME().Sleep(try_wait)
3090
- return [False,'Send fail({}) :\n{}'.format(sent[1],data)]
3100
+ return False,'Send fail({}) :\n{}'.format(sent[1],data),sock
3091
3101
 
3092
- def net_get_socket(host,port,timeout=3,dbg=0,SSLC=False): # host : Host name or IP
3102
+ def net_get_socket(host,port,timeout=3,dbg=6,SSLC=False,log=None): # host : Host name or IP
3093
3103
  try:
3094
3104
  af, socktype, proto, canonname, sa = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM)[0]
3095
3105
  except:
3096
- print('Can not get network informatin of {}:{}'.format(host,port))
3097
- return False
3106
+ _e_='Can not get network informatin of {}:{}'.format(host,port)
3107
+ return False,_e_
3098
3108
  try:
3099
3109
  soc = socket.socket(af, socktype, proto)
3100
3110
  if timeout > 0:
3101
3111
  soc.settimeout(timeout)
3102
3112
  except socket.error as msg:
3103
- print('could not open socket of {0}:{1}\n{2}'.format(host,port,msg))
3104
- return False
3113
+ _e_='could not open socket of {0}:{1}\n{2}'.format(host,port,msg)
3114
+ printf(_e_,log=log,dsp='e')
3115
+ return False,_e_
3105
3116
  ###### SSL Wrap ######
3117
+ _e_=None
3106
3118
  if SSLC:
3107
3119
  for i in range(0,5):
3108
3120
  icertfile='/tmp/.{}.{}.crt'.format(host,port)
@@ -3119,22 +3131,21 @@ def net_get_socket(host,port,timeout=3,dbg=0,SSLC=False): # host : Host name or
3119
3131
  try:
3120
3132
  soc=ssl.wrap_socket(soc,ca_certs=icertfile,cert_reqs=ssl.CERT_REQUIRED)
3121
3133
  soc.connect((host,port))
3122
- return soc
3134
+ return soc,'ok'
3123
3135
  except socket.error as msg:
3124
- if dbg > 3:
3125
- print(msg)
3136
+ printf(msg,log=log,log_level=dbg,mode='e')
3126
3137
  TIME().Sleep(1)
3127
3138
  ########################
3128
3139
  else:
3129
3140
  try:
3130
3141
  soc.connect(sa)
3131
- return soc
3142
+ return soc,'ok'
3132
3143
  except socket.error as msg:
3133
- if dbg > 3:
3134
- print('can not connect at {0}:{1}\n{2}'.format(host,port,msg))
3135
- return False
3144
+ _e_='can not connect at {0}:{1}\n{2}'.format(host,port,msg)
3145
+ printf(_e_,log=log,log_level=dbg,dsp='e')
3146
+ return False,_e_
3136
3147
 
3137
- def net_start_server(server_port,main_func_name,server_ip='',timeout=0,max_connection=10,log_file=None,certfile=None,keyfile=None):
3148
+ def net_start_server(server_port,main_func_name,server_ip='',timeout=0,max_connection=10,log_file=None,certfile=None,keyfile=None,log=None):
3138
3149
  ssoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3139
3150
  ssoc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
3140
3151
  if timeout > 0:
@@ -3142,10 +3153,10 @@ def net_start_server(server_port,main_func_name,server_ip='',timeout=0,max_conne
3142
3153
  try:
3143
3154
  ssoc.bind((server_ip, server_port))
3144
3155
  except socket.error as msg:
3145
- print('Bind failed. Error : {0}'.format(msg))
3156
+ printf('Bind failed. Error : {0}'.format(msg),log=log,mode='e',logfile=log_file)
3146
3157
  os._exit(1)
3147
3158
  ssoc.listen(max_connection)
3148
- print('Start server for {0}:{1}'.format(server_ip,server_port))
3159
+ printf('Start server for {0}:{1}'.format(server_ip,server_port),log=log,logfile=log_file)
3149
3160
  # for handling task in separate jobs we need threading
3150
3161
  while True:
3151
3162
  conn, addr = ssoc.accept()
@@ -3157,10 +3168,10 @@ def net_start_server(server_port,main_func_name,server_ip='',timeout=0,max_conne
3157
3168
  else:
3158
3169
  Thread(target=main_func_name, args=(conn, ip, port, log_file)).start()
3159
3170
  except:
3160
- print('No more generate thread for client from {0}:{1}'.format(ip,port))
3171
+ printf('No more generate thread for client from {0}:{1}'.format(ip,port),dsp='e',log=log,logfile=log_file)
3161
3172
  ssoc.close()
3162
3173
 
3163
- def net_start_single_server(server_port,main_func_name,server_ip='',timeout=0,max_connection=10,log_file=None,certfile=None,keyfile=None):
3174
+ def net_start_single_server(server_port,main_func_name,server_ip='',timeout=0,max_connection=10,log_file=None,certfile=None,keyfile=None,log=None):
3164
3175
  ssoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3165
3176
  ssoc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
3166
3177
  if timeout > 0:
@@ -3168,10 +3179,10 @@ def net_start_single_server(server_port,main_func_name,server_ip='',timeout=0,ma
3168
3179
  try:
3169
3180
  ssoc.bind((server_ip, server_port))
3170
3181
  except socket.error as msg:
3171
- print('Bind failed. Error : {0}'.format(msg))
3182
+ printf('Bind failed. Error : {0}'.format(msg),log=log,dsp='e',logfile=log_file)
3172
3183
  os._exit(1)
3173
3184
  ssoc.listen(max_connection)
3174
- print('Start server for {0}:{1}'.format(server_ip,server_port))
3185
+ printf('Start server for {0}:{1}'.format(server_ip,server_port),log=log,dsp='e',logfile=log_file)
3175
3186
  # for handling task in separate jobs we need threading
3176
3187
  conn, addr = ssoc.accept()
3177
3188
  ip, port = str(addr[0]), str(addr[1])
@@ -3326,14 +3337,14 @@ def cert_file(keyfile,certfile,C='US',ST='CA',L='San Jose',O='KGC',OU='KG',CN=No
3326
3337
  return key_file,crt_file
3327
3338
  return None,None
3328
3339
 
3329
- def net_put_data(IP,data,PORT=8805,key='kg',timeout=3,try_num=1,try_wait=[1,10],progress=None,enc=False,upacket=None,dbg=0,wait_time=3,SSLC=False):
3330
- sent=False,'Unknown issue'
3340
+ def net_put_data(IP,data,PORT=8805,key='kg',timeout=3,try_num=1,try_wait=[1,10],progress=None,enc=False,upacket=None,dbg=6,wait_time=3,SSLC=False,instant=True,log=None):
3341
+ sent=False,'Unknown issue',None
3331
3342
  for ii in range(0,try_num):
3332
3343
  if upacket: # Update packet function for number of try information ([#/<total #>])
3333
3344
  data=upacket('ntry',[ii+1,try_num],data)
3334
- sock=net_get_socket(IP,PORT,timeout=timeout,dbg=dbg,SSLC=SSLC)
3345
+ ok,sock=net_get_socket(IP,PORT,timeout=timeout,dbg=dbg,SSLC=SSLC,log=log)
3335
3346
 
3336
- if sock is False:
3347
+ if ok is False:
3337
3348
  if dbg >= 3:
3338
3349
  print('Can not get socket data [{}/{}], wait {}s'.format(ii+1,try_num,wait_time))
3339
3350
  else:
@@ -3341,21 +3352,21 @@ def net_put_data(IP,data,PORT=8805,key='kg',timeout=3,try_num=1,try_wait=[1,10],
3341
3352
  sys.stdout.flush()
3342
3353
  TIME().Sleep(wait_time)
3343
3354
  continue
3344
- sent=False,'Unknown issue'
3355
+ sent=False,'Unknown issue',sock
3345
3356
  try:
3346
- sent=net_send_data(sock,data,key=key,enc=enc)
3357
+ sent=net_send_data(sock,data,key=key,enc=enc,log=log)
3347
3358
  except:
3348
- print('send fail, try again ... [{}/{}]'.format(ii+1,try_num))
3359
+ printf('send fail, try again ... [{}/{}]'.format(ii+1,try_num),log=log)
3349
3360
  if sent[0]:
3350
- if sock:
3361
+ if sock and instant:
3351
3362
  sock.close()
3352
- return [True,'sent']
3363
+ sock=None
3364
+ return True,'sent',sock
3353
3365
  if try_num > 1:
3354
3366
  wait_time=Random(length=0,strs=try_wait,mode='int')
3355
- if dbg >= 3:
3356
- print('try send data ... [{}/{}], wait {}s'.format(ii+1,try_num,wait_time))
3367
+ printf('try send data ... [{}/{}], wait {}s'.format(ii+1,try_num,wait_time),log=log,log_level=dbg)
3357
3368
  TIME().Sleep(wait_time)
3358
- return [False,'Send fail({}) :\n{}'.format(sent[1],data)]
3369
+ return False,'Send fail({}) :\n{}'.format(sent[1],data),sock
3359
3370
 
3360
3371
 
3361
3372
  def encode(string):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kmisc
3
- Version: 2.1.87
3
+ Version: 2.1.88
4
4
  Summary: Enginering useful library
5
5
  Home-page: https://github.com/kagepark/kmisc
6
6
  Author: Kage Park
File without changes
File without changes
File without changes
File without changes
File without changes