IronPython integration with C#/ .NET

2020-05-04 22:59发布

问题:

So I'm trying to develop this application that will get a captcha image from an website and try to decode it so I can fill the captcha input text with the resulted text after I decode the picture.

I want to use the solution I found here: http://www.wausita.com/captcha/ So, before trying to integrate it in my main application I tried to integrate it in a simple console application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace Python
{
    class Program
    {
        static void Main(string[] args)
        {
                var ipy = IronPython.Hosting.Python.CreateRuntime();
                dynamic test = ipy.ExecuteFile("crack.py");
        }
    }
}

When I run a simple test.py file with just a print "Hello" it works, but when I run my "crack.py" file I get the following error:

   Message: {"Object reference not set to an instance of an object."}
    Source: "Microsoft.Dynamic"
    StackTrace:    at Microsoft.Scripting.Actions.Calls.MethodCandidate.Caller.Call(Object[] args, Boolean& shouldOptimize)
   at IronPython.Runtime.Types.BuiltinFunction.BuiltinFunctionCaller`5.Call4(CallSite site, CodeContext context, TFuncType func, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute6[T0,T1,T2,T3,T4,T5,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
   at IronPython.Runtime.Importer.Import(CodeContext context, String fullName, PythonTuple from, Int32 level)
   at IronPython.Runtime.Operations.PythonOps.ImportWithNames(CodeContext context, String fullName, String[] names, Int32 level)
   at Microsoft.Scripting.Utils.InvokeHelper`5.Invoke(Object arg0, Object arg1, Object arg2, Object arg3)
   at Microsoft.Scripting.Utils.ReflectedCaller.Invoke(Object[] args)
   at Microsoft.Scripting.Interpreter.CallInstruction.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.RunInstructions(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
   at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
   at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
   at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
   at Microsoft.Scripting.SourceUnit.Execute(Scope scope)
   at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
   at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteFile(String path, ScriptScope scope)
   at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteFile(String path)
   at Microsoft.Scripting.Hosting.ScriptRuntime.ExecuteFile(String path)
   at Python.Program.Main(String[] args) in E:\Python\Python\Program.cs:line 20

This is the crack.py file

from PIL import Image
import hashlib
import time
import os
import math


class VectorCompare:
  def magnitude(self, concordance):
    total = 0
    for word,count in concordance.iteritems():
      total += count ** 2
    return math.sqrt(total)

  def relation(self, concordance1, concordance2):
    relevance = 0
    topvalue = 0
    for word, count in concordance1.iteritems():
      if concordance2.has_key(word):
        topvalue += count * concordance2[word]
    return topvalue / (self.magnitude(concordance1) * self.magnitude(concordance2))

f = open('workfile.txt', 'w')

def buildvector(im):
  d1 = {}

  count = 0
  for i in im.getdata():
    d1[count] = i
    count += 1

  return d1

v = VectorCompare()

iconset = ['0','1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

imageset = []

 for letter in iconset:
  for img in os.listdir('./iconset/%s/' % (letter)):
    temp = []
    if img != "Thumbs.db": # windows check...
      temp.append(buildvector(Image.open("./iconset/%s/%s" % (letter, img))))
    imageset.append({letter:temp})

im = Image.open("captcha1.gif")
im2 = Image.new("P", im.size, 255)
im = im.convert("P")
temp = {}

for x in range(im.size[1]):
  for y in range(im.size[0]):
    pix = im.getpixel((y, x))
    temp[pix] = pix
    if pix == 0 or pix == 0: # these are the numbers to get
      im2.putpixel((y, x), 0)

inletter = False
foundletter = False
start = 0
end = 0

letters = []

for y in range(im2.size[0]): # slice across
  for x in range(im2.size[1]): # slice down
    pix = im2.getpixel((y, x))
    if pix != 255:
      inletter = True

  if foundletter == False and inletter == True:
    foundletter = True
    start = y

  if foundletter == True and inletter == False:
    foundletter = False
    end = y
    letters.append((start, end))

  inletter=False

count = 0
for letter in letters:
  m = hashlib.md5()
  im3 = im2.crop((letter[0], 0, letter[1], im2.size[1]))

  guess = []

  for image in imageset:
    for x, y in image.iteritems():
      if len(y) != 0:
        guess.append((v.relation(y[0], buildvector(im3)), x))

  guess.sort(reverse=True)
  print>>f, guess[0]
  count += 1

My question is: what is causing this error and how can I make the python call work? Or, is there a way to replace the Python file "creack.py" functionality using C#?

回答1:

You can create a function to run your IronPythonScript and catch and re-raise exceptions that tell you what line of code is problematic as follows:

public static dynamic RunIronPythonScript(string fileName)
{
    var ipy = IronPython.Hosting.Python.CreateRuntime();
    try
    {
        dynamic test = ipy.ExecuteFile(fileName);
        return test;
    }
    catch (Exception e)
    {
        var engine = IronPython.Hosting.Python.GetEngine(ipy);
        ExceptionOperations eo = engine.GetService<ExceptionOperations>();
        string error = eo.FormatException(e);
        throw new Exception(error);
    }
}

Then you can call it as follows:

 static void Main(string[] args)
 {
    RunIronPythonScript("crack.py");
 }

This will at least show you which line of code is causing the error, making it easier to fix the script up.



回答2:

Try:

Microsoft.Scripting.Hosting.ScriptEngine engine =
     IronPython.Hosting.Python.CreateEngine();

engine.ExecuteFile("crack.py");