-
Notifications
You must be signed in to change notification settings - Fork 224
Open
Description
Hello jeff, long time no see.last time I may not descript the problem clearly,so I bring the code this time.
When I run Java program to drive Python script at 2.7.3 or2.7.4b1,the title error comes out.the solution is add str() or json.dumps() to request body. when it still exists on 2.7.4b1'version,so I think if we may on 'different ship' or it will fix on 2.7.4' release version?
sorry for the the format of code,but u can figure it out.
thank u.
the Python script is like this:
import copy
import json
import urllib2
from contextlib import closing
def send_urllib_req(**kwargs):
param = dict()
request_body = {"a": "A"}
# under 2.7.2,comment out this line or not,the code works find
# under 2.7.3 or 2.7.4b1,comment out this line,the error will come out
# request_body = json.dumps(request_body)
# request_body = str(request_body)
param["data"] = request_body
url = "http://127.0.0.1:12345/demo/hello"
param["url"] = url
request = urllib2.Request(**param)
request.get_method = lambda: "POST"
with closing(urllib2.urlopen(request)) as response:
code = response.code
body = response.read()
return
send_urllib_req()
the Java code is:
import org.apache.commons.io.FileUtils;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
public class JythonTest {
private static ScriptEngine ENGINE = new ScriptEngineManager().getEngineByName("python");
private static void test() throws ScriptException, IOException {
// content of test_jython.py is python code above
File f = new File("D:\\project\\SpringTest\\SpringDemo\\src\\main\\resources\\python\\test_jython.py");
String content = FileUtils.readFileToString(f, Charset.defaultCharset());
Bindings bindings = ENGINE.createBindings();
ENGINE.eval(content, bindings);
Object res = bindings.get("data");
}
public static void main(String[] args) throws ScriptException, IOException {
test();
}
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels