2012-07-29 147 views
1

我有一個運行PHP和CURL的應用程序。 我的想法是將應用程序移動到Python-Django-Requests。 我一直無法工作,希望你能幫我一把。 該應用程序的工作原理如下: 收集:一個數字,一條消息並通過API發送一條短信。請求模塊Python

PHP代碼。 http://pastebin.com/PqpBgstD

import requests 
import cookielib 
posdata = "p_num_text=00513015924048&smstemplate=&message=message_sending&txtcount=8 
      +char+%3A+1+Sms&hiddcount=152" 
jar = cookielib.CookieJar() 
user = 'xxx' 
pass = 'xxx' 
values = {'app': 'page', 'inc': 'login', 'op': 'auth_login', 
      'username': user, 'password': pass} # data login 
r = requests.post(url, data=values, cookies=jar) # Login 
values = {'app': 'menu', 'inc': 'send_sms', 
      'op': 'sendsmstopv_yes'}# values ​​to enter to send the sms 
r = requests.post(url, data=values, params=posdata, cookies=jar)# enter the area sms 
print r.content 

我怎麼能在捲曲通過代碼來請求?

以上代碼是否正常?

回答

3

您的代碼將無法正常工作,我已經把它貼在下面的更正後的代碼,請注意,您不需要使用cookielib作爲Requestscookie會生成一個CookieJar對象。

import requests 

url = "http://dominio.com/subdominio/index.php" 

username = 'xxx' 
password = 'xxx' 

payload = { 
    'app': 'page', 
    'inc': 'login', 
    'op': 'auth_login', 
    'username': username, 
    'password': password} 

r = requests.post(url, data=payload) # Login 

cSMS = "Sms" 

payload = { 
    'p_num_text': '00513015924048', 
    'smstemplate': '', 
    'message': 'message_sending', 
    'txtcount': '8', 
    'char': cSMS, # your php code seems to be off for this one, double check it 
    'hiddcount': '153'} 

url = "http://dominio.com/subdominio/index.php?app=menu&inc=send_sms&op=sendsmstopv_yes" 

r = requests.post(url, data=payload, cookies=r.cookies) # enter the area sms 

print r.text