perl的int为什么不识别_
时间:2011-07-26
来源:互联网
             python嵌入的问题
大家好:
为了实现c---python互操作,我写了一个py脚本。
在shell下调试,工作正常。
初始化c的程序正常。
我使用的py是3.0的。
但导入from urllib.request import urlopen后,原先的py无法导入了。
请教有什么问题?
文件名PyUtil.py
import sys
from urllib.request import urlopen 有了这个引用后,导入失败
CRLF = '\r\n'
def addValue(oper1,oper2): 只有这个函数的时候,导入是正常的
return (oper1 + oper2);
def ConnectURL(url):
try:
req = urlopen(url)
response = req.read()
req.close()
return (req.getcode(),req.geturl())
except BaseException as exp:
sys.stdout.write(exp)
#ConnectURL("http://www.msn.com.cn")
本地代码如下:
#include "python.h"
BOOL AddValueWrap(PyObject* pPyUtil,int oper1,int oper2,int* nRet)
{
PyObject* pPypfn = NULL;
PyObject* pPyArgs = NULL;
PyObject* pPyRet = NULL;
BOOL bExec = FALSE;
// addValue
pPypfn = PyObject_GetAttrString(pPyUtil,"addValue");
if(pPypfn != NULL)
{
// set oper
pPyArgs = Py_BuildValue("(i,i)",oper1,oper2);
if(pPyArgs != NULL)
{
// exec
pPyRet = PyEval_CallObject(pPypfn,pPyArgs);
if(pPyRet != NULL && pPyRet !=Py_None)
{
// get return data
if(PyArg_ParseTuple(pPyRet,"i",nRet) == 0)
{
bExec = TRUE;
}
Py_DECREF(pPyRet);
}
Py_DECREF(pPyArgs);
}
Py_DECREF(pPypfn);
}
return bExec;
}
int main()
{
PyObject* pPyUtil = NULL;
PyObject* pPypfn = NULL;
PyObject* pPyArgs = NULL;
PyObject* pPyRet = NULL;
int nRet = 0;
int nValue = 0;
Py_Initialize();
PyImport_ImportModule("sys");
PyImport_ImportModule("urllib");
pPyUtil = PyImport_ImportModuleEx("PyUtil",Py_None,Py_None,Py_None); // 导入失败
if(pPyUtil != NULL)
{
AddValueWrap(pPyUtil,20,30,&nValue);
Py_DECREF(pPyUtil);
}
Py_Finalize();
}
            大家好:
为了实现c---python互操作,我写了一个py脚本。
在shell下调试,工作正常。
初始化c的程序正常。
我使用的py是3.0的。
但导入from urllib.request import urlopen后,原先的py无法导入了。
请教有什么问题?
文件名PyUtil.py
import sys
from urllib.request import urlopen 有了这个引用后,导入失败
CRLF = '\r\n'
def addValue(oper1,oper2): 只有这个函数的时候,导入是正常的
return (oper1 + oper2);
def ConnectURL(url):
try:
req = urlopen(url)
response = req.read()
req.close()
return (req.getcode(),req.geturl())
except BaseException as exp:
sys.stdout.write(exp)
#ConnectURL("http://www.msn.com.cn")
本地代码如下:
#include "python.h"
BOOL AddValueWrap(PyObject* pPyUtil,int oper1,int oper2,int* nRet)
{
PyObject* pPypfn = NULL;
PyObject* pPyArgs = NULL;
PyObject* pPyRet = NULL;
BOOL bExec = FALSE;
// addValue
pPypfn = PyObject_GetAttrString(pPyUtil,"addValue");
if(pPypfn != NULL)
{
// set oper
pPyArgs = Py_BuildValue("(i,i)",oper1,oper2);
if(pPyArgs != NULL)
{
// exec
pPyRet = PyEval_CallObject(pPypfn,pPyArgs);
if(pPyRet != NULL && pPyRet !=Py_None)
{
// get return data
if(PyArg_ParseTuple(pPyRet,"i",nRet) == 0)
{
bExec = TRUE;
}
Py_DECREF(pPyRet);
}
Py_DECREF(pPyArgs);
}
Py_DECREF(pPypfn);
}
return bExec;
}
int main()
{
PyObject* pPyUtil = NULL;
PyObject* pPypfn = NULL;
PyObject* pPyArgs = NULL;
PyObject* pPyRet = NULL;
int nRet = 0;
int nValue = 0;
Py_Initialize();
PyImport_ImportModule("sys");
PyImport_ImportModule("urllib");
pPyUtil = PyImport_ImportModuleEx("PyUtil",Py_None,Py_None,Py_None); // 导入失败
if(pPyUtil != NULL)
{
AddValueWrap(pPyUtil,20,30,&nValue);
Py_DECREF(pPyUtil);
}
Py_Finalize();
}
作者: febwave 发布时间: 2011-07-26
             你的C程序完全编译不通过。 
python.h是个啥(是Python.h吧)?BOOL又是个啥?
我的建议是:PyImport_ImportModule("urllib.request"); 如何?
            
python.h是个啥(是Python.h吧)?BOOL又是个啥?
我的建议是:PyImport_ImportModule("urllib.request"); 如何?
作者: cuihao 发布时间: 2011-07-26
 相关阅读 更多  
      
    热门阅读
-   office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具 office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具阅读:74 
-   如何安装mysql8.0 如何安装mysql8.0阅读:31 
-   Word快速设置标题样式步骤详解 Word快速设置标题样式步骤详解阅读:28 
-   20+道必知必会的Vue面试题(附答案解析) 20+道必知必会的Vue面试题(附答案解析)阅读:37 
-   HTML如何制作表单 HTML如何制作表单阅读:22 
-   百词斩可以改天数吗?当然可以,4个步骤轻松修改天数! 百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!阅读:31 
-   ET文件格式和XLS格式文件之间如何转化? ET文件格式和XLS格式文件之间如何转化?阅读:24 
-   react和vue的区别及优缺点是什么 react和vue的区别及优缺点是什么阅读:121 
-   支付宝人脸识别如何关闭? 支付宝人脸识别如何关闭?阅读:21 
-   腾讯微云怎么修改照片或视频备份路径? 腾讯微云怎么修改照片或视频备份路径?阅读:28 















