Beispiel #1
0
    def getPublicIp():
        # get socket object #
        socket = SysMgr.getPkg('socket', False)
        if not socket:
            return

        from socket import socket, AF_INET, SOCK_DGRAM, SOCK_STREAM

        ret = None

        try:
            s = socket(AF_INET, SOCK_STREAM)
            s.settimeout(0.3)

            # connect to google public IP #
            s.connect(("8.8.8.8", 53))

            ret = s.getsockname()[0]
        except SystemExit:
            sys.exit(0)
        except:
            SysMgr.printWarn("Fail to get public IP address")

        if not ret:
            ret = NetworkMgr.getMainIp()

        return ret
Beispiel #2
0
    def recvfrom(self, size=0, noTimeout=False, verbose=True):
        if self.ip is None or self.port is None:
            SysMgr.printErr(\
                "Fail to use IP address for server because it is not set")
            return False
        elif not self.socket:
            SysMgr.printErr(\
                "Fail to use socket for client because it is not set")
            return False

        # get socket object #
        socket = SysMgr.getPkg('socket', False)

        # set recv size #
        if size == 0:
            size = self.recvSize

        while 1:
            try:
                message, address = self.socket.recvfrom(size)
                return (message, address)
            except socket.timeout:
                if noTimeout:
                    continue
                SysMgr.printWarn(\
                    "Fail to receive data from %s:%d as client because %s" % \
                    (self.ip, self.port, 'timeout'))
                return None
            except KeyboardInterrupt:
                sys.exit(0)
            except SystemExit:
                sys.exit(0)
            except:
                if verbose:
                    SysMgr.printWarn(\
                        "Fail to receive data from %s:%d as client because %s" % \
                            (self.ip, self.port, SysMgr.getErrReason()))
                return None
Beispiel #3
0
    def recv(self, size=0):
        if self.ip is None or self.port is None:
            SysMgr.printErr(\
                "Fail to use IP address for server because it is not set")
            return False
        elif not self.socket:
            SysMgr.printErr(\
                "Fail to use socket for client because it is not set")
            return False

        # set recv size #
        if size == 0:
            size = self.recvSize

        try:
            return self.socket.recv(size)
        except SystemExit:
            sys.exit(0)
        except:
            err = SysMgr.getErrReason()
            SysMgr.printWarn(\
                "Fail to receive data from %s:%d as client because %s" % \
                (self.ip, self.port, err))
            return False
Beispiel #4
0
    def setServerNetwork(\
        ip, port, force=False, blocking=False, tcp=False, anyPort=False):
        if SysMgr.localServObj and not force:
            SysMgr.printWarn(\
                "Fail to set server network because it is already set")
            return

        # get internet available IP first #
        if not ip:
            ip = NetworkMgr.getPublicIp()

        # print available IP list #
        try:
            iplist = sorted(NetworkMgr.getUsingIps())
            if len(iplist) > 0:
                SysMgr.printWarn(\
                    'available IP list [%s]' % ', '.join(iplist))
        except:
            pass

        # check server setting #
        if SysMgr.localServObj and \
            SysMgr.localServObj.socket and \
            SysMgr.localServObj.ip == ip and \
            SysMgr.localServObj.port == port:
            if blocking:
                SysMgr.localServObj.socket.setblocking(1)
            else:
                SysMgr.localServObj.socket.setblocking(0)
            return

        # create a new server setting #
        networkObject = NetworkMgr(\
            'server', ip, port, blocking, tcp, anyPort)
        if not networkObject.ip:
            SysMgr.printWarn(\
                "Fail to set server IP", True)
            return

        if tcp:
            proto = 'TCP'
        else:
            proto = 'UDP'

        SysMgr.localServObj = networkObject
        SysMgr.printInfo(\
            "use %s:%d(%s) as local address" % \
            (SysMgr.localServObj.ip, \
                SysMgr.localServObj.port, proto))

        return networkObject
Beispiel #5
0
    def getServerConn():
        def printErr():
            SysMgr.printErr(\
                "No running server or wrong server address")

        # set server address in local #
        if SysMgr.isLinux and not SysMgr.remoteServObj:
            try:
                addr = SysMgr.getProcAddrs(__module__)
            except:
                addr = None

            if not addr:
                return None

            # classify ip and port #
            service, ip, port = NetworkMgr.parseAddr(addr)
            if service == ip == port == None:
                printErr()
                return None
            else:
                NetworkMgr.setRemoteServer(addr, tcp=True)
        # set server address again #
        elif SysMgr.remoteServObj:
            servObj = SysMgr.remoteServObj
            ip = servObj.ip
            port = servObj.port
            NetworkMgr.setRemoteServer('%s:%s' % (ip, port), tcp=True)

        # check server address #
        if not SysMgr.remoteServObj:
            printErr()
            return None

        # bind local socket for UDP #
        try:
            if not SysMgr.remoteServObj.tcp and \
                SysMgr.localServObj:
                lip = SysMgr.localServObj.ip
                lport = SysMgr.localServObj.port
                SysMgr.remoteServObj.socket.bind((lip, lport))
        except:
            err = SysMgr.getErrReason()
            SysMgr.printErr(\
                "Fail to bind socket to %s:%s for connection because %s" % \
                    (lip, lport, err))

        # do connect to server #
        try:
            connObj = SysMgr.remoteServObj

            connObj.timeout()

            # connect with handling CLOSE_WAIT #
            while 1:
                try:
                    connObj.connect()
                    break
                except:
                    err = SysMgr.getErrReason()
                    SysMgr.printWarn(\
                        "Fail to connect to %s:%s because %s" % \
                            (ip, port, err))
                    if err.startswith('99'):
                        time.sleep(0.1)
                        continue
                    break

            return connObj
        except:
            err = SysMgr.getErrReason()
            SysMgr.printErr(\
                "Fail to set socket for connection because %s" % err)
            return None