3
蟒蛇2.7.10(./Watcher/epMain.py
):C++把Python的urllib
import subprocess
import hashlib
import os
import sys
import zipfile
import httplib
#import urllib
#import urllib2
def letsbegin():
subprocess.call('a.exe')
httpClient = httplib.HTTPConnection('www.google.com', 80, timeout=30)
httpClient.request('GET', '/updata/Client_V.html')
response = httpClient.getresponse()
targetV = response.read()
letsbegin()
C++:
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./Watcher')");
PyObject *pyMain = PyImport_ImportModule("epMain")
的pyMain
總是NULL
但之後,我改變我的Python代碼:
import subprocess
import hashlib
import os
import sys
import zipfile
#import httplib
#import urllib
#import urllib2
def letsbegin():
subprocess.call('a.exe')
httpClient = httplib.HTTPConnection('www.google.com', 80, timeout=30)
httpClient.request('GET', '/updata/Client_V.html')
response = httpClient.getresponse()
targetV = response.read()
letsbegin()
那麼可以在我的C++代碼中加載這個模塊 但我真的想在這個項目中使用httplib,怎麼樣?我不能使用:
PyImport_ImportModule("httplib")
因爲python代碼可能會經常更新。 此外,當我使用
d:\pros\go\Watcher>python epMain.py
它的工作原理!
urllib
和urllib2
也有這樣的問題。
DEF試驗(): 返回內容sys.version C++: 的PyObject *結果= PyObject_CallMethod (pyMain,「test」,NULL); (Result) \t { \t \t \t char * s = PyString_AsString(Result); \t \t \t printf(s); \t} //返回2.7.10 – rookie