Using requests module with ironpython

I am hoping someone can help me out. I’ve been trying to use the requests module with ironpython to post data to our API. Originally I was just using CPython which works great, but I am hoping to take advantage of the wpf forms @eirannejad builtin to pyrevit. I have full_frame set to true in the bundle metadata.

import requests

response_1 = requests.get('https://httpbin.org/get')
print(response_1.status_code)

response_2 = requests.get('https://bimservice.ssgbim.com/')
print(response_2.status_code)

When I run this with CPython I get 200 codes for both, but when I run it with ironpython I get a 200 code for the first request and an exception with the second.

IOError: System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

Hey Kyle, have you been able to find a solution in the meantime? I have a similar issue!

The error could have been on the server side. Not necessarly in the code.
I tried it, both in Cpython and Ironpython and both worked returning 200

1 Like

Cool! In what IronPython engine are you able to run requests? I struggle to do so still as seen in this post

2711

Ok great, thanks. Do you know how you can switch ironpython engine/version outside of the pyrevit settings? Using the CLI perhaps? That setting seems disabled for me, as seen in the screenshot of my other post.

in CLI

Usage:

    pyrevit attach (-h | --help)
    pyrevit attach <clone_name> (default | <engine_version>) (<revit_year> | --installed | --attached) [--allusers] [--log=<log_file>]

    Arguments & Options:
        <clone_name>             Name of target clone
        <revit_year>             Revit version year e.g. 2019
        <engine_version>         Engine version to be used e.g. 277
        latest                   Use latest engine
        dynamosafe               Use latest engine that is compatible with DynamoBIM
        --installed              All installed Revits
        --attached               All currently attached Revits
        --allusers               Attach for all users

no idea why it is disabled in your install, @eirannejad would know
I looked in the issues on the github and did not find anything related

1 Like

I ended up using Webclient instead of requests

from System.Net import WebClient

client = WebClient()
client.Headers.Add("Authorization", auth)
client.Headers.Add("Accept", "application/json")
client.Headers.Add("Content-Type", "application/json")
response = client.UploadString(url, "POST", data)
1 Like

I came across this post but for another reason - import requests module not found error.
In case others run into this issue of module not found In ironpython 3.4 ,
I was able to resolve by making sure search path for modules has entry for correct site-packages
Maybe I didnt follow all the installation steps correctly - not sure but the path to site-packages was missing fixed be code below

import sys
sys.path.append (r'C:\Users\user\AppData\Roaming\pyRevit-Master\site-packages')
import requests
2 Likes