How To Create a GPRS Connection ?

Help
2010-03-22
2013-05-30
  • Hi,
      I have a app where I need send a file by FTP. My SmartPhone is a Palm Treo Pro with Windows Mobile 6.1 Professional.
      I have a problem: How to detect and create a 3G/GPRS connection ?

      When I try execute this code, it don't connect/create/estabilished a connection with the FTP Server:
      ftp = ftplib.FTP("ftp.myserver.com.br")

      ftp.login("userName", "password")

      ftp.set_pasv(0)

      ftp.set_debuglevel(2)

      ftp.sendcmd("CWD /public_ftp/incoming")

      upload(ftp, myFile)

       To solve this I have that open the "Internet Explorer" before execute my app and this is bad.
     
        How to solve this problem ?

        Thanks Leandro.

     
  • Here is the Code that works for me:

    # 20100705154437 
    # Manfred Schulte-Oversohl :: Pragmatis GmbH
    # Windows Mobile GPRS Connection
    from ctypes import *
    import time
    import sys
    from socket import *
    # Let's map the Microsoft types to ctypes for clarity
    BYTE      = c_ubyte
    WORD      = c_ushort
    DWORD     = c_ulong
    class GUID(Structure):
        _fields_ = [("Data1", DWORD),
                    ("Data2", WORD),
                    ("Data3", WORD),
                    ("Data4", BYTE * 8)]
                    
        def __init__(self, name=None):
            if name is not None:
                oledll.ole32.CLSIDFromString(unicode(name), byref(self))
        def __str__(self):
            s = (c_wchar * 39)()
            oledll.ole32.StringFromGUID2(byref(self), s, 39)
            return s.value
    class ConnectionInfo(Structure):
        _fields_ = [
        ("cbSize", c_uint),
        ("dwParams", c_uint),
        ("dwFlags", c_uint),
        ("dwPriority", c_uint),
        ("bExclusive", c_int),
        ("bDisabled", c_int),
        ("guidDestNet", GUID),
        ("hWnd", c_void_p),
        ("uMsg", c_uint),
        ("lParam", c_uint),
        ("ulMaxCost", c_uint),
        ("ulMinRcvBw", c_uint),
        ("ulMaxConnLatency", c_uint),
        ]
        
    S_OK = 0
    CONNMGR_PARAM_GUIDDESTNET = 0x1
    CONNMGR_FLAG_PROXY_HTTP = 0x1
    CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000
    INFINITE = 0xffffffff
    CONNMGR_STATUS_CONNECTED = 0x10
    ht = {}
    cellcore = cdll.cellcore
    def Setup(urlStr):
        ci = ConnectionInfo()
        phConnection = c_void_p()
        status = c_int()
        if urlStr in ht:
            return True
        dest = GUID()
        ret = cellcore.ConnMgrMapURL(unicode(urlStr), byref(dest), 0)
        writeLog_File("GUID of dest: %s" % str(dest))
        if  ret != S_OK:
            return False
        ci.cbSize = sizeof(ci);
        ci.dwParams = CONNMGR_PARAM_GUIDDESTNET
        ci.dwFlags = CONNMGR_FLAG_PROXY_HTTP
        ci.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE
        ci.guidDestNet = dest
        ci.bExclusive = 0
        ci.bDisabled = 0
        ci.hWnd = None
        ci.uMsg = 0
        ci.lParam = 0
        
        if cellcore.ConnMgrEstablishConnectionSync(byref (ci), byref( phConnection), INFINITE, byref( status)) != S_OK and status != CONNMGR_STATUS_CONNECTED:
            return False
        writeLog_File("L087 ConnMgrEstablishConnectionSync True")
        ht[urlStr] = phConnection
        writeLog_File("len (ht) %s" % str(len (ht)))
        return True
    def DoTcpConnection():
        '''
        With VPN setting firstly the Internet Connection via ISP will be established
        and then the VPN connection will be initialized
        '''
        url = "asus"    # dummy, name triggers VPN with settings for Work
        ##url = "www.company.com" # for Internet via ISP
        ##res = Setup("http://www.name.com/") # Internet Pattern
        ##res = Setup("http://name/") # Work Pattern (without Dots)
        
        res = Setup("http://%s" % url)
        if res:
            writeLog_File("connected to %s" % url)
            # Begin Socket
            s = socket(AF_INET, SOCK_STREAM)
            s.connect(('192.168.0.48', 8000)) # IP / Port of Destination PC in Work via VPN
            s.settimeout(5)
            s.send('Hello world via VNP')
            data = s.recv(1024)
            s.close()
            # End Socket
            writeLog_File('Received: >%s<' % str(data))
        else:
            writeLog_File("Connection establishment failed")
        
    def writeLog_File(log_txt = '', log_file = '', debug=0):
        if not log_file:
            if sys.argv[0].endswith(".py"): 
                log_file = sys.argv[0].replace('.py','.log')
            else:
                log_file = "%s.log" % sys.argv[0]
        if log_file != '':
            fhnd = open(log_file, 'a')
            fhnd.write("%s\t" % str(time.strftime( '%Y%m%dT%H%M%S.', time.localtime()) + str(time.time()).split('.')[1]))
            if log_txt != '':
                fhnd.write("%s\n" % (log_txt))
            if log_txt == '':
                fhnd.write("\n")
            fhnd.close()
    if __name__ == "__main__":
        DoTcpConnection()
    

    Enjoy
    Manfred

     
  • Manfred,

       I'll test your code !.
      
       Thank you so much for your replay.

    -Leandro