2012-04-15 32 views
0

我正在努力獲得Rest API Post與供應商api一起工作,並希望有人可以給我一個指針。Python將數據添加到帖子主體

意圖是將cli命令提供給帖子主體並傳遞給返回輸出的設備。

調用看起來是這樣的:(這適用於所有其他的電話,但,這是因爲張貼到身體的不同)

def __init__(self,host,username,password,sid,method,http_meth): 
    self.host=host 
    self.username= username 
    self.password= password 
    self.sid=sid 
    self.method=method 
    self.http_meth=http_meth 

    def __str__(self): 
    self.url = 'http://' + self.host + '/rest/' 
    self.authparams = urllib.urlencode({ "session_id":self.sid,"method": self.method,"username": self.username, 
     "password": self.password, 
    }) 

call = urllib2.urlopen(self.url.__str__(), self.authparams).read() 
return (call) 

無論怎樣,我都試過這個,我不能讓它正常工作。以下是解釋如何使用此方法的API文檔的摘錄:

要處理這些API,請將您的CLI命令放置在HTTP緩衝區中,然後將方法名稱,會話ID和其他參數放入網址。

任何人都可以給我一個如何正確做到這一點的想法。我不是開發人員,並且正在努力正確地學習這一點。例如,如果我想在郵件正文中發送命令「help」?

感謝您的任何指導

+1

你可能會更好使用請求庫的http://文檔。 python-requests.org/en/latest/index.html它工作在比urllib更高的層次上,並且更易於使用。 – 2012-04-15 13:18:50

+0

是作爲URL的一部分傳遞的身份驗證參數嗎?我的意思是像url一樣?sessionid = ...&... – 2012-04-15 13:43:13

+0

我同意湯姆,使用請求,溝urllib2 – Wes 2012-04-15 15:42:37

回答

0

好吧,這是可笑的簡單,我是在思維這一點。我發現我有時可以看到比真正的問題和腰圍更高的水平。無論如何,這是它應該是如何工作的:

def cli(self,method): 
    self.url = ("http://" + str(self.host) + "/rest//?method=" +str(method)+ "&username=" +str(self.username)+ "&password=" +str(self.password)+ "&enable_password=test&session_id="+str(self.session_id)) 
    data="show ver" 
    try: 
     req = urllib2.Request(self.url) 
     response = urllib2.urlopen(req,data) 
     result = response.read() 
     print result 

    except urllib2.URLError, e: 
     print e.reason 

的CLI命令只是放在緩衝區,而不是編碼....