def delete_records(self, file_no, records_no): try: socket.setdefaulttimeout(10) conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect((self.hostaddr, self.port)) for record_no in records_no: # print record_no if isinstance(record_no, str): try: record_no = int(record_no) except ValueError as ex: record_no = 0 request_data = ReqData().create_request_data( ReqData.Del_Record, file_no, record_no).decode("hex") conn.send(request_data) ack_data = conn.recv(1) if not ack_data: raise TwsException("No Ack") ack = ord(ack_data[0]) try: if ack == 0xE1: raise TwsException("Write Error") if ack == 0xE2: raise TwsException("No Data") if ack != 0x06: raise TwsException("Unknown Error") except TwsException, e: # print e # traceback.print_exc() common.log_err(self.hostname + ' - ' + str(e)) except socket.error, e: # traceback.print_exc() common.log_err(self.hostname + ' - ' + str(e)) return False
def dele_by_name(self, name): try: remote_file = name self.ftp.delete_file(remote_file) return True except Exception, e: common.log_err(traceback.format_exc()) return False
def send_file(self, master, created_file): try: remote_file = master.scale_file_name self.ftp.upload_file(created_file, remote_file) return True except Exception, e: common.log_err(traceback.format_exc()) return False
def dele(self, master): try: # ftp = smftp(self.ip, self.usr, self.pwd) # ftp.login() self.ftp.delete_file(master.scale_file_name) # time.sleep(0.3) return True except Exception, e: common.log_err(traceback.format_exc()) return False
def recv_file(self, master): try: # local_file = self.ip + "_" + master.scale_file_name local_file = self.get_scale_file_name_entire(master) remote_file = master.scale_file_name self.ftp.download_file(local_file, remote_file) return local_file except Exception, e: common.log_err(traceback.format_exc()) return ""
def download_file(self, file_no): file_name = "%s.%02x.dat" % (self.hostname, file_no) file_name = os.path.join(common.get_current_directory(), file_name) try: socket.setdefaulttimeout(10) conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect((self.hostaddr, self.port)) record_no = 0 with open(file_name, "w") as fp: while True: request_data = ReqData().create_request_data( ReqData.Read_File, file_no, record_no) request_data = request_data.decode("hex") conn.send(request_data) received_data = conn.recv(1460) received_length = len(received_data) if received_length == 1: ack = ord(received_data[0]) if ack == 0xE0: raise TwsException("Error on Reading") if ack == 0xE2: break else: received_data = received_data.encode("hex").upper() if not received_data: common.log_err("Received data is Empty") break # ¼ÆË㳤¶È real_size = int(received_data[8:12], 16) while real_size > received_length: # for STE48 3.00+ # if received_length % 1460 == 0: conn.send('\x06') # received_data_plus = conn.recv(1460) if not received_data_plus: break received_data_plus_length = len(received_data_plus) received_data_plus = received_data_plus.encode( "hex").upper() received_length += received_data_plus_length received_data += received_data_plus # try: record_no = int(received_data[:8]) except Exception, e: record_no = int(received_data[:8], 16) fp.write(received_data + "\r\n") except TwsException, e: # print e common.log_err(self.hostname + ' - ' + str(e)) return ""
def create_csv(self, master): try: # local_file = self.ip + "_" + master.scale_file_name local_file = self.get_scale_file_name_entire(master) local_file = os.path.join(common.get_current_directory(), local_file) master.to_csv(local_file) return local_file except Exception, e: common.log_err(traceback.format_exc()) return ""
def recv(self, master): try: # local_file = self.ip + "_" + master.scale_file_name local_file = self.get_scale_file_name_entire(master) remote_file = master.scale_file_name # ftp = smftp(self.ip, self.usr, self.pwd) # ftp.login() self.ftp.download_file(local_file, remote_file) # time.sleep(0.1) master.from_csv(local_file, with_head=False) return True except Exception, e: common.log_err(traceback.format_exc()) return False
def send(self, master): try: # local_file = self.ip + "_" + master.scale_file_name local_file = self.get_scale_file_name_entire(master) remote_file = master.scale_file_name master.to_csv(local_file) # ftp = smftp(self.ip, self.usr, self.pwd) # try: # ftp.login() self.ftp.upload_file(local_file, remote_file) # time.sleep(0.3) return True # except Exception as e: # print e except Exception, e: common.log_err(traceback.format_exc()) return False
def delete_file(self, file_no): request_data = ReqData().create_request_data(ReqData.Del_File, file_no, 0).decode("hex") try: socket.setdefaulttimeout(10) conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect((self.hostaddr, self.port)) conn.send(request_data) ack_data = conn.recv(1) if not ack_data: raise TwsException("No Ack") ack = ord(ack_data[0]) if ack == 0xE1: raise TwsException("Write Error") if ack == 0xE2: raise TwsException("No Data") if ack != 0x06: raise TwsException("Unknown Error") except socket.error, e: # print e common.log_err(self.hostname + ' - ' + str(e)) return False
def upload_file(self, file_no, file_name): request_data = ReqData().create_request_data(ReqData.Write_File, file_no, 0) # tmpline = "" try: # socket.setdefaulttimeout(10) socket.setdefaulttimeout(6) conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect((self.hostaddr, self.port)) with open(file_name, "r") as fp: for line in fp: data_send = request_data + line.rstrip() data_send = data_send.decode("hex") # conn.send(data_send) # ack_data = conn.recv(1) # if not ack_data: # raise TwsException("No Ack") # ack = ord(ack_data[0]) ack_data = None from socket import error as socket_error for i in range(3): try: conn.send(data_send) ack_data = conn.recv(1) except socket_error, e: common.log_err(self.hostname + ' - ' + str(e)) if ack_data: break if not ack_data: raise TwsException("No Ack") ack = ord(ack_data[0]) if ack == 0xE1: raise TwsException("Write Error") if ack == 0xE3: raise TwsException("No Free Space") if ack == 0xE2: raise TwsException("No Data") if ack != 0x06: raise TwsException("Unknown Error") except socket.error, e: common.log_err(self.hostname + ' - ' + str(e)) # print "socket error" # traceback.print_exc() # print tmpline # print e return False
def send_by_name(self, name, data): try: local_file = self.ip + "_" + name local_file = os.path.join(common.get_current_directory(), local_file) remote_file = name with open(local_file, "wb") as fp: fp.write(data) # ftp = smftp(self.ip, self.usr, self.pwd) # try: # ftp.login() self.ftp.upload_file(local_file, remote_file) # time.sleep(0.3) return True # except Exception as e: # print e except Exception, e: common.log_err(traceback.format_exc()) return False
def login(self): ftp = self.ftp connected = False try: timeout = 20 socket.setdefaulttimeout(timeout) # ftp.set_pasv(True) ftp.set_pasv(False) # print u'开始连接到 %s' %(self.hostaddr) ftp.connect(self.hostaddr, self.port) # print u'成功连接到 %s' %(self.hostaddr) # print u'开始登录到 %s' %(self.hostaddr) ftp.login(self.username, self.password) # print u'成功登录到 %s' %(self.hostaddr) # debug_print(ftp.getwelcome()) connected = True except Exception: common.log_err('Connecting Or Login Failed on %s' % self.hostaddr) return connected
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect((self.hostaddr, self.port)) conn.send(request_data) ack_data = conn.recv(1) if not ack_data: raise TwsException("No Ack") ack = ord(ack_data[0]) if ack == 0xE1: raise TwsException("Write Error") if ack == 0xE2: raise TwsException("No Data") if ack != 0x06: raise TwsException("Unknown Error") except socket.error, e: # print e common.log_err(self.hostname + ' - ' + str(e)) return False except TwsException, e: # print e common.log_err(self.hostname + ' - ' + str(e)) return False else: return True def delete_records(self, file_no, records_no): try: socket.setdefaulttimeout(10) conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect((self.hostaddr, self.port)) for record_no in records_no: # print record_no if isinstance(record_no, str): try: record_no = int(record_no) except ValueError as ex:
if info: ms = info['ProductVersionMS'] ls = info['ProductVersionLS'] print "Version: {0}.{1}.{2}.{3} ".format( HIWORD(ms), LOWORD(ms), HIWORD(ls), LOWORD(ls)) else: print "unknown version" print "Shanghai Teraoka Co,. Ltd" sys.exit(0) else: sys.exit(1) except getopt.GetoptError as err: common.log_err("Parameters Error: %s" % err) file_list = [] common.set_title_onoff(optTitling) # Checking Confliction conflict_check_list_file = [] conflict_check_list_import = [] if optImportForPlu: conflict_check_list_import.append(optImportForPlu) if optImportForDateTime: conflict_check_list_import.append(optImportForDateTime) if optImportForTrace: