问题描述
所以我想开发这个应用程序会从网站获得captcha图像,并尝试去code,所以我可以在我去code中的填充所得文本验证码输入文本图片。
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");
}
}
}
当我运行只需打印你好它的工作原理,但是当我跑我的crack.py文件我得到以下错误的简单test.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
这是crack.py文件
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
我的问题是:是什么原因造成这个错误,我怎么可以让蟒蛇电话的工作?
或者说,有没有办法使用C#来代替Python的文件creack.py功能?
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#?
推荐答案
尝试:
Microsoft.Scripting.Hosting.ScriptEngine engine =
IronPython.Hosting.Python.CreateEngine();
engine.ExecuteFile("crack.py");
这篇关于用C#IronPython的集成/ .NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!