def send_udp_package(self, dst_ip, dst_port, send_msg="hello,world!"): """ 功能描述:发送udp报文到UDP Server,并接收回应,验证链路是否通 参数: dst_ip: UDP报文的目的IP dst_port: UDP报文的目的port send_msg: UDP报文的消息内容 返回值: ATTUDPCLIENT_SUC, log_data 表示通 ATTUDPCLIENT_FAIL, log_data 表示不通 ATTUDPCLIENT_PORT_BUSY, log_data 表示端口被占用 """ # 创建子进程启动UDP Client try: sub_process = Process(target=self._send_udp_package, args=(dst_ip, dst_port, send_msg, self.connection_status)) sub_process.start() sub_process.join() except Exception, e: log_data = "Start UDP Client occurs exception,error message is: %s" % e log.debug_err(log_data) return ATTUDPCLIENT_FAIL, log_data
def send_tcp_package(self, dst_ip, dst_port, send_msg="hello,world!"): """ 功能描述:发送tcp报文到TCP Server,并接收回应,验证链路是否通 参数: dst_ip: TCP报文的目的IP dst_port: TCP报文的目的port send_msg: TCP报文的消息内容 返回值: ATTTCPCLIENT_SUC, log_data 表示通 ATTTCPCLIENT_FAIL, log_data 表示不通 ATTTCPCLIENT_PORT_BUSY, log_data 表示端口被占用 """ # 创建子进程启动TCP client并发送报文 try: sub_process = Process(target=self._send_tcp_package, args=(dst_ip, dst_port, send_msg, self.connection_status)) sub_process.start() sub_process.join() except Exception, e: log_data = "Send TCP package occurs exception, error info is: %s" % e log.debug_err(log_data) return ATTTCPCLIENT_FAIL, log_data
def debug_err(log_string): try: global DEBUG_FLAG if not DEBUG_FLAG: log.debug_err(log_string) _log_string(log_string) except Exception, e: pass
def start_udp_server(self, ip, port): """ 功能描述:开启UDP Server 参数: ip: UDP server所在主机IP地址 port: UDP Server监听的端口号 """ # 创建子进程启动UDP Server try: sub_process = Process(target=self.obj.start_udp_server, args=(ip, port, self.flag, self.error)) sub_process.start() except Exception, e: log_data = "Start UDP Server occurs exception,error message is: %s" % e log.debug_err(log_data) return ATTUDPSERVER_FAIL, log_data
def _connect_ftp(self, host, port=21, username='', password='', timeout=20): """ 功能描述:连接并登录FTP服务器 参数: host: FTP服务器地址 port: FTP服务器监听端口,默认为21 username: 连接服务器的用户名,空表示匿名登录,一般服务器对匿名登录的用户有权限限制 password: 连接服务器的密码 timeout: 连接超时时长,单位为秒 返回: 成功返回:(ATTFTPCLENT_SUC,成功信息) 失败返回:(ATTFTPCLENT_FAIL,失败信息) """ #add by jias 20130808 if self.login: info = u"已经连接并登录FTP服务器" return ATTFTPCLENT_SUC, info try: # 创建对象,并连接服务器 self.ftp = ftplib.FTP() ret_info = self.ftp.connect(host, int(port), int(timeout)) log.debug_info(u"连接FTP服务器成功.详细信息为:%s" % ret_info) except Exception, e: ret_info = u"连接FTP服务器发生异常:%s" % e log.debug_err(ret_info) #add by jias 20130808 恢复conn前状态 if self.ftp: self.ftp.close() self.ftp = None return ATTFTPCLENT_FAIL, ret_info
log.debug_info(log_data) reactor.listenTCP(port, factory, interface=ip) reactor.callLater(1, self._check_flag, flag) reactor.run() except Exception, e: log_data = "ListenTCP occurs exception, error info is %s" % e log.debug_info(log_data) flag.value = 3 # 开启服务器监听发生异常 error.value = log_data else: log_data = "The addr %s:%s have been used, choose another port please!" % ( ip, str(port)) log.debug_err(log_data) flag.value = 2 # 端口被占用 def stop_tcp_server(self, flag): """ 关闭TCPServer """ flag.value = 0 class ATTTCPServer(object): """ ATTTCPServer """
class ATTFtpClient(): def __init__(self, port): """ initial """ self.ftp = None self.ftp_server_encode = 'GBK' self.port = int(port) self.login = False def clear(self): """ 清空连接和标志 """ try: if self.ftp: self.ftp.quit() #在server关闭后,请求断开,会报异常 except Exception: pass finally: self.ftp = None self.login = False self.ftp_server_encode = 'GBK' def _connect_ftp(self, host, port=21, username='', password='', timeout=20): """ 功能描述:连接并登录FTP服务器 参数: host: FTP服务器地址 port: FTP服务器监听端口,默认为21 username: 连接服务器的用户名,空表示匿名登录,一般服务器对匿名登录的用户有权限限制 password: 连接服务器的密码 timeout: 连接超时时长,单位为秒 返回: 成功返回:(ATTFTPCLENT_SUC,成功信息) 失败返回:(ATTFTPCLENT_FAIL,失败信息) """ #add by jias 20130808 if self.login: info = u"已经连接并登录FTP服务器" return ATTFTPCLENT_SUC, info try: # 创建对象,并连接服务器 self.ftp = ftplib.FTP() ret_info = self.ftp.connect(host, int(port), int(timeout)) log.debug_info(u"连接FTP服务器成功.详细信息为:%s" % ret_info) except Exception, e: ret_info = u"连接FTP服务器发生异常:%s" % e log.debug_err(ret_info) #add by jias 20130808 恢复conn前状态 if self.ftp: self.ftp.close() self.ftp = None return ATTFTPCLENT_FAIL, ret_info try: ret_info = self.ftp.login(username, password) self.login = True return ATTFTPCLENT_SUC, ret_info except Exception, e: ret_info = u"登陆FTP服务器发生异常:%s" % e log.debug_err(ret_info) #add by jias 20130808 恢复conn前状态 if self.ftp: self.ftp.close() self.ftp = None return ATTFTPCLENT_FAIL, ret_info