目前有个特殊的需求,不管异步还是同步,nodejs貌似都不能装上session发起请求用。 当前nodejs没有用在web中,我是用在工具层的调度链条中。 后来看到python.js,经过测试发现,python.js可以实现在python中的一些功能。 所以就调用python去做带session的请求部分。 结果发现安装的时候各种错误。。。
../src/py_object_wrapper.h:43:66: error: ‘PropertyCallbackInfo’ does not name a type
static void ValueOfAccessor(Local<String> js_property, const PropertyCallbackInfo<Value>& js_info);
^
../src/py_object_wrapper.h:43:66: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
../src/py_object_wrapper.h:43:86: error: expected ‘,’ or ‘...’ before ‘<’ token
static void ValueOfAccessor(Local<String> js_property, const PropertyCallbackInfo<Value>& js_info);
^
../src/py_object_wrapper.h:44:67: error: ‘PropertyCallbackInfo’ does not name a type
static void ToStringAccessor(Local<String> js_property, const PropertyCallbackInfo<Value>& js_info);
^
../src/py_object_wrapper.h:44:67: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
../src/py_object_wrapper.h:44:87: error: expected ‘,’ or ‘...’ before ‘<’ token
static void ToStringAccessor(Local<String> js_property, const PropertyCallbackInfo<Value>& js_info);
^
../src/py_object_wrapper.h:46:28: error: ‘FunctionCallbackInfo’ does not name a type
static void Call(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:46:28: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
../src/py_object_wrapper.h:46:48: error: expected ‘,’ or ‘...’ before ‘<’ token
static void Call(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:47:31: error: ‘FunctionCallbackInfo’ does not name a type
static void ValueOf(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:47:31: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
../src/py_object_wrapper.h:47:51: error: expected ‘,’ or ‘...’ before ‘<’ token
static void ValueOf(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:48:32: error: ‘FunctionCallbackInfo’ does not name a type
static void ToString(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:48:32: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
../src/py_object_wrapper.h:48:52: error: expected ‘,’ or ‘...’ before ‘<’ token
static void ToString(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:58:38: error: ‘FunctionCallbackInfo’ does not name a type
Handle<Value> InstanceCall(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:58:38: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
../src/py_object_wrapper.h:58:58: error: expected ‘,’ or ‘...’ before ‘<’ token
Handle<Value> InstanceCall(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:59:41: error: ‘FunctionCallbackInfo’ does not name a type
Handle<Value> InstanceValueOf(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:59:41: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
../src/py_object_wrapper.h:59:61: error: expected ‘,’ or ‘...’ before ‘<’ token
Handle<Value> InstanceValueOf(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:60:42: error: ‘FunctionCallbackInfo’ does not name a type
Handle<Value> InstanceToString(const FunctionCallbackInfo<Value>& js_args);
^
../src/py_object_wrapper.h:60:42: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
../src/py_object_wrapper.h:60:62: error: expected ‘,’ or ‘...’ before ‘<’ token
Handle<Value> InstanceToString(const FunctionCallbackInfo<Value>& js_args);
^
../src/binding.cc:11:19: error: ‘FunctionCallbackInfo’ does not name a type
void import(const FunctionCallbackInfo<Value>& args)
^
../src/binding.cc:11:19: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
../src/binding.cc:11:39: error: expected ‘,’ or ‘...’ before ‘<’ token
void import(const FunctionCallbackInfo<Value>& args)
^
../src/binding.cc: In function ‘void import(int)’:
../src/binding.cc:15:23: error: ‘args’ was not declared in this scope
HandleScope scope(args.GetIsolate());
^
../src/binding.cc:19:30: error: ‘NewFromUtf8’ is not a member of ‘v8::String’
Exception::Error(String::NewFromUtf8(args.GetIsolate(), "I don't know how to import that."))));
^
../src/binding.cc: In function ‘void init(v8::Handle<v8::Object>)’:
../src/binding.cc:38:44: error: no matching function for call to ‘v8::HandleScope::HandleScope(v8::Isolate*)’
HandleScope scope(Isolate::GetCurrent());
^
../src/binding.cc:38:44: note: candidates are:
In file included from /root/.node-gyp/0.8.0/src/node.h:62:0,
from ../src/binding.cc:2:
/root/.node-gyp/0.8.0/deps/v8/include/v8.h:467:3: note: v8::HandleScope::HandleScope(const v8::HandleScope&)
HandleScope(const HandleScope&);
^
/root/.node-gyp/0.8.0/deps/v8/include/v8.h:467:3: note: no known conversion for argument 1 from ‘v8::Isolate*’ to ‘const v8::HandleScope&’
/root/.node-gyp/0.8.0/deps/v8/include/v8.h:442:3: note: v8::HandleScope::HandleScope()
HandleScope();
^
/root/.node-gyp/0.8.0/deps/v8/include/v8.h:442:3: note: candidate expects 0 arguments, 1 provided
../src/binding.cc:43:18: error: ‘NewFromUtf8’ is not a member of ‘v8::String’
exports->Set(String::NewFromUtf8(Isolate::GetCurrent(), "import", String::kInternalizedString),
^
../src/binding.cc:43:71: error: ‘kInternalizedString’ is not a member of ‘v8::String’
exports->Set(String::NewFromUtf8(Isolate::GetCurrent(), "import", String::kInternalizedString),
^
../src/binding.cc:44:60: error: no matching function for call to ‘v8::FunctionTemplate::New(v8::Isolate*, void (&)(int))’
FunctionTemplate::New(Isolate::GetCurrent(), import)->GetFunction());
^
../src/binding.cc:44:60: note: candidate is:
In file included from /root/.node-gyp/0.8.0/src/node.h:62:0,
from ../src/binding.cc:2:
/root/.node-gyp/0.8.0/deps/v8/include/v8.h:2225:34: note: static v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::InvocationCallback, v8::Handle<v8::Value>, v8::Handle<v8::Signature>)
static Local<FunctionTemplate> New(
^
/root/.node-gyp/0.8.0/deps/v8/include/v8.h:2225:34: note: no known conversion for argument 1 from ‘v8::Isolate*’ to ‘v8::InvocationCallback {aka v8::Handle<v8::Value> (*)(const v8::Arguments&)}’
../src/binding.cc:48:98: error: no matching function for call to ‘v8::Local<v8::FunctionTemplate>::New(v8::Isolate*, v8::Persistent<v8::FunctionTemplate>&)’
Local<FunctionTemplate>::New(Isolate::GetCurrent(), PyObjectWrapper::py_function_template);
^
../src/binding.cc:48:98: note: candidate is:
In file included from /root/.node-gyp/0.8.0/src/node.h:62:0,
from ../src/binding.cc:2:
/root/.node-gyp/0.8.0/deps/v8/include/v8.h:4030:10: note: static v8::Local<T> v8::Local<T>::New(v8::Handle<T>) [with T = v8::FunctionTemplate]
Local<T> Local<T>::New(Handle<T> that) {
^
/root/.node-gyp/0.8.0/deps/v8/include/v8.h:4030:10: note: candidate expects 1 argument, 2 provided
../src/binding.cc:49:18: error: ‘NewFromUtf8’ is not a member of ‘v8::String’
exports->Set(String::NewFromUtf8(Isolate::GetCurrent(), "PyObject", String::kInternalizedString),
^
../src/binding.cc:49:73: error: ‘kInternalizedString’ is not a member of ‘v8::String’
exports->Set(String::NewFromUtf8(Isolate::GetCurrent(), "PyObject", String::kInternalizedString),
^
make: *** [Release/obj.target/binding/src/binding.o] Error 1
make: Leaving directory `/root/python.js/build'
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/data/apps/node-v6.2.0-linux-x64/lib/node_modules/node-gyp/lib/build.js:276:23)
gyp ERR! stack at emitTwo (events.js:106:13)
gyp ERR! stack at ChildProcess.emit (events.js:191:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:204:12)
gyp ERR! System Linux 2.6.32-279.19.27.el6.ucloud.x86_64
gyp ERR! command "/data/apps/node-v6.2.0-linux-x64/bin/node" "/apps/node-v6.2.0-linux-x64/bin/node-gyp" "rebuild" "--arch=[ia32/x64]" "--target=0.8.0"
gyp ERR! cwd /root/python.js
gyp ERR! node -v v6.2.0
gyp ERR! node-gyp -v v3.4.0
gyp ERR! not ok
先上下自己撸出来的模块~摸索出来的~
/**
* Author: Smarttang
* Date: 2016.10.14
*
* ------
* 基于python的带session请求
* 可以请求post和get
*/
var python = require('python.js');
var requests = python.import('requests');
var log = require('./logUtils');
module.exports = {
/**
* 初始化session请求对象
* @return {[type]} [description]
*/
init: function(){
this.session = requests.session();
},
/**
* GET请求(带有session)
* @param {[type]} interface [description]
* @param {[type]} url [description]
* @return {[type]} [description]
*/
get: function(interface,url){
try {
var response = this.session.get(url);
return JSON.parse(response.text);
} catch(e) {
// 异常处理的地方
log.reqPackageError(interFace,url,e.stack);
}
},
/**
* POST请求(带有session)
* @param {[type]} interface [description]
* @param {[type]} url [description]
* @param {[type]} data [description]
* @return {[type]} [description]
*/
post: function(interface,url,data){
var response;
try {
response = this.session.post(url,data=data);
return JSON.parse(response.text);
} catch(e) {
// 异常处理的地方
log.reqPackageError(interFace,url,e.stack);
}
},
/**
* POST请求(带有session)上传
* @param {[type]} interface [description]
* @param {[type]} url [description]
* @param {[type]} data [description]
* @param {[type]} file [description]
* @return {[type]} [description]
*/
post_upload: function(interface,url,data,file){
var response;
try {
response = this.session.post(url,files=file,data=data);
return JSON.parse(response.text);
} catch(e) {
// 异常处理的地方
log.reqPackageError(interFace,url,e.stack);
}
}
};
翻了好多文档,看了很多基本上都不成功,不知道有没有大神已经测试过?求指教下,鄙人感激不尽~