簡體   English   中英

c ++ embed python urllib

[英]c++ embed python urllib

python 2.7.10( ./Watcher/epMain.py Watcher/ ./Watcher/epMain.py ):

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

有用!
urlliburllib2也有這樣的問題。

看起來你用Python 3.x包含/ libs而不是2.x編譯。

在Python 3.x中, httpliburllib2不可用。 (它們被重命名為http.client' and urllib.request , urllib.error`)

更改編譯選項以包含,鏈接Python 2.x.

UPDATE

要檢查C ++程序使用的版本,請嘗試以下代碼:

Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("print(sys.version)");
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM