How can I use requests module

Options
sombodyfromtheworld
sombodyfromtheworld Member Posts: 29
First Comment Name Dropper

With this python version IronPython 2.7.0.40 on .NET 4.0.30319.42000
how can I use requests module. How basically to install it?

Tagged:

Comments

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 804
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Options

    Hi @sombodyfromtheworld , from a quick Google and Stackoverflow search this seems to be a CPython module, it will not work in IronPython.

  • sombodyfromtheworld
    sombodyfromtheworld Member Posts: 29
    First Comment Name Dropper
    Options

    I would like to post here a solution for some who still wants to use Python 2.7 and be able to make HTTP requests without requests module.
    I was looking for a solution to send quick log messages to my telegram bot during Ansys process and found .NET libraries that can make HTTP requests. Here is a complete code to send quick log messages to telegram BOT:

    import clr
    clr.AddReference("System.Web.Extensions")
    
    from System.Web.Script.Serialization import JavaScriptSerializer
    from System import Net
    
    
    def telegram_notification(msg):
        try:
            client = Net.WebClient()
            bot_token = '<YOUR_TELEGRAM_TOKEN>'
            bot_chatID = '<YOUR_TELEGRAM_CHAT_ID>'
            url = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + msg
            response_bytes = client.DownloadData(url)
            response_text = client.Encoding.GetString(response_bytes)
            serializer = JavaScriptSerializer()
            response_json = serializer.DeserializeObject(response_text)
            return response_json
        except Net.WebException as e:
            return None
        except Exception as e:
            return None
    
    # call it anywhere like this
    print(telegram_notification('Workbench is active'))
    
  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 804
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Options

    @sombodyfromtheworld this is awesome, thank you, thanks for sharing!