Results 1 to 6 of 6

Thread: Translate PYTHON code to VB .NET

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    1

    Translate PYTHON code to VB .NET

    Hello,

    Does someone could help me translating the following PYTHON code to VB .NET ?

    Thank you very much

    Code:
    #!/usr/bin/python
    
    '''
    A brief example of some of the NordVPN api calls - simply dumps JSON results
    '''
    
    import requests 
    import json
    import hashlib
    import argparse
    
    
    parser = argparse.ArgumentParser(description='nordtoy')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-u','--userdata', nargs='*', dest='userdetails', help='my@email  mypassword', default='')
    group.add_argument('-l','--serverload', nargs='*', dest='serverload', help='server domain', default='')
    group.add_argument('-s','--serverstats', action='store_true')
    group.add_argument('-d','--serverdetail',  action='store_true')
    group.add_argument('-c','--config', action='store_true')
    group.add_argument('-a','--address', action='store_true')
    group.add_argument('-n','--nameserver', action='store_true')
    
    
    ns=parser.parse_args()
    
    # Get the indiviual server load
    if ns.serverload:
        response = requests.get(
                url = 'https://api.nordvpn.com/server/stats/'+ns.serverload[0],
                headers = {
                'User-Agent': 'NordVPN_Client_5.56.780.0',
                'Host': 'api.nordvpn.com',  
                'Connection':'Close'
            }
            )
        
        print (response.text)
    # Get user account details, using verified token    
    elif ns.userdetails:
        response = requests.get(
            url = 'https://api.nordvpn.com/token/token/'+ns.userdetails[0],
            headers = {
            'User-Agent': 'NordVPN_Client_5.56.780.0',
            'Host': 'api.nordvpn.com',  
            'Connection':'Close'
        }
        )
        json_data=json.loads(response.text)
        #Uncomment next line if you want to see the JSON token, salt, key, response
        #print(response.text)
       
        #The response to validate the token is - salt+password, SHA512 hashed - then that hash+key, SHA512 hashed again
        firsthash=hashlib.sha512(json_data['salt'].encode()+ns.userdetails[1].encode())
        secondhash=hashlib.sha512(firsthash.hexdigest()+json_data['key'].encode())
        
        verifyurl='https://api.nordvpn.com/token/verify/'+json_data['token']+'/'+secondhash.hexdigest()
        # Validate token    
        response = requests.get(
            url = verifyurl,
            headers = {
            'User-Agent': 'NordVPN_Client_5.56.780.0',
            'Host': 'api.nordvpn.com',  
            'Connection':'Close'
        }
        )
        # Returns true if validated correctly
        print(response.text)
        # Display account details    
        response = requests.get(
            url = 'https://api.nordvpn.com/user/databytoken',
            headers = {
            'User-Agent': 'NordVPN_Client_5.56.780.0',
            'Host': 'api.nordvpn.com',  
            'nToken': json_data['token'],
            'Connection':'Close'
        }
        )
            
        print (response.text)
    # Get current loads for all servers   
    elif ns.serverstats:
        response = requests.get(
            url = 'https://api.nordvpn.com/server/stats',
            headers = {
            'User-Agent': 'NordVPN_Client_5.56.780.0',
            'Host': 'api.nordvpn.com',  
            'Connection':'Close'
        }
        )
            
        print (response.text)
    # Get detailed information on all servers    
    elif ns.serverdetail:
        response = requests.get(
            url = 'https://api.nordvpn.com/server',
            headers = {
            'User-Agent': 'NordVPN_Client_5.56.780.0',
            'Host': 'api.nordvpn.com',  
            'Connection':'Close'
        }
        )
            
        print (response.text)
    # Download OpenVPN config files for all servers    
    elif ns.config:
        response = requests.get(
            url = 'https://api.nordvpn.com/files/zipv2',
            headers = {
            'User-Agent': 'NordVPN_Client_5.56.780.0',
            'Host': 'api.nordvpn.com',  
            'Connection':'Close'
        }
        )
        
        with open("config.zip", "wb") as code:
            code.write(response.content)
    # Get users current IP address   
    elif ns.address:
        response = requests.get(
            url = 'https://api.nordvpn.com/user/address',
            headers = {
            'User-Agent': 'NordVPN_Client_5.56.780.0',
            'Host': 'api.nordvpn.com',  
            'Connection':'Close'
        }
        )
            
        print (response.text)
    #Display NordVPN nameservers    
    elif ns.nameserver:
        response = requests.get(
            url = 'https://api.nordvpn.com/dns/smart',
            headers = {
            'User-Agent': 'NordVPN_Client_5.56.780.0',
            'Host': 'api.nordvpn.com',  
            'Connection':'Close'
        }
        )
            
        print (response.text)
        
        
    else:
        print("No options selected, options are -\n")
        print("-u, --userdata     -  -u my@email mypassword  - gives account details")
        print("-l, --serverload   -  -l au1.nordvpn.com      - shows server load as percentage ")
        print("-s, --serverstats  -  -s - lists server load for all servers")
        print("-d, --serverdetail -  -d - lists detailed information about all servers")
        print("-c, --config       -  -c - downloads OpenVPN config files for all servers to config.zip")
        print("-a, --address      -  -a - displays users IP address")
        print("-n, --nameserver   -  -n - displays NordVPN nameservers")

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Translate PYTHON code to VB .NET

    Eh, I could, but it's a non-trivial conversion.

    The argument parsing alone would be 20-30 lines of VB minimum, unless I find a NuGet package to do the same thing as 'argparse'. I guess in hindsight I could do it in 8-10 lines but it won't do exactly the same thing in all scenarios if I cut that corner.

    The next bit makes a REST request and does a handful of things with the JSON it returns. The request/parse structure is going to be anything from 10-100 lines of code depending on how you count and if you feel like doing it "right".

    The most complex branch is 'userdetails'. It generates a pair of hashes, then uses that data to make two other REST requests. That's all said and done another 20-30 lines.

    I think it'd take me between 1-2 hours to translate it as a console application, and more like 5-10 if you wanted a GUI. Then, if I wanted to teach you something, I'd spend 2-3 more hours minimum on the material. 3-12 hours is an awful lot to ask of a stranger.

    If you have the Python and it works, why bother? What motivates translating it to VB .NET? Have you tried translating it yourself? What parts did you get stuck on?
    Last edited by Sitten Spynne; Dec 12th, 2017 at 02:02 PM.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Translate PYTHON code to VB .NET

    Quote Originally Posted by erty33 View Post
    Does someone could help me translating the following PYTHON code to VB .NET ?
    People often say "can you help with this" when what they actually mean is "can you do this for me". Trying to convert code from one language to another by simple replacement of "commands" is a good way to end up with unusable code, just as doing the same with spoken/written languages can end up producing gibberish. The best way to go is to analyse the functionality provided by the existing code and then determine the best way to provide that functionality in the target language. We can certainly help with specific issues along the way but if you're looking for someone to volunteer their time to just write the code for you then I expect that you'll be disappointed. Are you perhaps a VB developer who found some PHP code on the web that reportedly does what they want and you want to avoid having to understand PHP?

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Translate PYTHON code to VB .NET

    Quote Originally Posted by jmcilhinney View Post
    Are you perhaps a VB developer who found some PHP code on the web that reportedly does what they want and you want to avoid having to understand PHP?
    Python. If it was PHP the type names would be like "ArgemuntPraser" or more idiomatic "preg_parser()". And the whole script wouldn't work without this at the top:
    Code:
    $six = 7
    $seven = 6
    PHP is like Perl only if you dropped Perl on its head a few times as a baby.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Translate PYTHON code to VB .NET

    Quote Originally Posted by Sitten Spynne View Post
    Python. If it was PHP...
    Oops! Just started doing some PHP work at the office and had it in mind. They both begin with P so what's the difference?

  6. #6
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,168

    Re: Translate PYTHON code to VB .NET

    You can manage to do some converting yourself, I have done this with JS to VB and the other way around.

    You start by what does the code do, and not just it adds 1+1, litterally breaking it down function by function, and then converting it to the equivlent of vb.net.

    jmc, doesn't mean any harm when he says, that when you ask for help you mean do it for you, but its true we are all guilty of it at some point even myself included.

    You can convert it by asking these questions:

    1: What does it do at the end result?

    2: What is used to complete the operation?

    3: Can this be done simplier.


    I can answer almost all of these questions, but it wouldn't be 110% right, and only a guess.

    The first thing is you are printing information about a server, this could be anything from just pinging to finding out how old a page is.

    What us used here is a If Then, Else operation in vb.net it looks like this:

    Code:
    If X = Y then
    
    Y = X 
    Elseif Y = X then
    
    X = Y
    
    Else
    
    console.write("Error X = Y returned false")
    
    End If
    For the web requests, its very similar to what you have you simply create a new web request, you can find information on web requests in this article: https://docs.microsoft.com/en-us/dot...brequest-class


    Yes, it is in C#, but as stated in another thread C# can be converted with a free converter online
    Disclaimer: When code is given for example - it is merely a example.




    Unless said otherwise indicated - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width