2010-07-02 15 views
6

का उपयोग कर एक्सएमएल पेलोड के साथ प्रमाणीकृत HTTP पोस्ट, मैं आयरनपीथन में urllib2 का उपयोग कर एक पूरी तरह से एक्सएमएल पेलोड (मुझे लगता है) के साथ एक पोस्ट संदेश भेजने की कोशिश कर रहा हूं। हालांकि, हर बार जब मैं इसे भेजता हूं, तो यह त्रुटि कोड 400 (खराब अनुरोध) देता है।पाइथन urllib2

मैं वास्तव में mimick करने के लिए एक Boxee को दूर कतार आइटम कॉल जिसके लिए वास्तविक डाटा पैकेट (WireShark से) इस तरह दिखता है कोशिश कर रहा हूँ:

POST /action/add HTTP/1.1 
User-Agent: curl/7.16.3 (Windows build 7600; en-US; beta) boxee/0.9.21.11487 
Host: app.boxee.tv 
Accept: */* 
Accept-Encoding: deflate, gzip 
Cookie: boxee_ping_version=9; X-Mapping-oompknoc=76D730BC9E858725098BF13AEFE32EB5; boxee_app=e01e36e85d368d4112fe4d1b6587b1fd 
Connection: keep-alive 
Content-Type: text/xml 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Accept-Language: en-us,en;q=0.5 
Keep-Alive: 300 
Connection: keep-alive 
Content-Length: 53 

<message type="dequeue" referral="3102296"></message> 

मैं पोस्ट भेजने के लिए निम्नलिखित अजगर कोड का उपयोग कर रहा :

def PostProtectedPage(theurl, username, password, postdata): 

    req = urllib2.Request(theurl, data=postdata) 
    req.add_header('Content-Type', 'text/xml') 
    try: 
     handle = urllib2.urlopen(req) 
    except IOError, e:     # here we are assuming we fail 
     pass 
    else:        # If we don't fail then the page isn't protected 
     print "This page isn't protected by authentication." 
     sys.exit(1) 

    if not hasattr(e, 'code') or e.code != 401:     # we got an error - but not a 401 error 
     print "This page isn't protected by authentication." 
     print 'But we failed for another reason.' 
     sys.exit(1) 

    authline = e.headers.get('www-authenticate', '')    # this gets the www-authenticat line from the headers - which has the authentication scheme and realm in it 
    if not authline: 
     print 'A 401 error without an authentication response header - very weird.' 
     sys.exit(1) 

    authobj = re.compile(r'''(?:\s*www-authenticate\s*:)?\s*(\w*)\s+realm=['"](\w+)['"]''', re.IGNORECASE)   # this regular expression is used to extract scheme and realm 
    matchobj = authobj.match(authline) 
    if not matchobj:          # if the authline isn't matched by the regular expression then something is wrong 
     print 'The authentication line is badly formed.' 
     sys.exit(1) 
    scheme = matchobj.group(1) 
    realm = matchobj.group(2) 
    if scheme.lower() != 'basic': 
     print 'This example only works with BASIC authentication.' 
     sys.exit(1) 

    base64string = base64.encodestring('%s:%s' % (username, password))[:-1] 
    authheader = "Basic %s" % base64string 
    req.add_header("Authorization", authheader) 
    try: 
     handle = urllib2.urlopen(req) 
    except IOError, e:     # here we shouldn't fail if the username/password is right 
     print "It looks like the username or password is wrong." 
     print e 
     sys.exit(1) 
    thepage = handle.read() 
    return thepage 

बहरहाल, जब भी मैं इस चलाने के लिए, यह त्रुटि 400 (ख़राब अनुरोध) रिटर्न
मुझे पता है क्योंकि मैं इसे कहीं और का उपयोग करें (और मैं इसे कल्पना नहीं कर सकते कतार लाने के लिए प्रमाणीकरण सही है उपयोग नहीं किया जाता , अन्यथा यह कैसे होगा डब्ल्यू किस खाते में परिवर्तन लागू करने के लिए?)

नेटवर्क कैप्चर को देखते हुए, क्या मैं अनुरोध में कुछ हेडर जोड़ना याद कर सकता हूं? शायद कुछ आसान है, लेकिन मुझे यह जानने के लिए कि क्या है, पाइथन या HTTP अनुरोधों के बारे में पर्याप्त जानकारी नहीं है।

संपादित: Btw, मैं कोड के रूप में इस फोन कर रहा हूँ (यह वास्तव में गतिशील है, लेकिन यह मूल विचार है):

PostProtectedPage("http://app.boxee.tv/action/add", "user", "pass", "<message type=\"dequeue\" referral=\"3102296\"></message>") 
+1

एक टिप। पहले कमांड लाइन पर कर्ल का उपयोग करके इसे काम करने का प्रयास करें। आप कर्ल के डीबग विकल्पों का उपयोग करके उस पर कब्जा कर सकते हैं और इससे आपको कुछ संकेत मिल सकते हैं। यह भी संभव है कि अजगर में आपको कर्ल उपयोगकर्ता-एजेंट शीर्षलेख बनाने की आवश्यकता है क्योंकि दुर्भाग्य से कुछ प्रोग्रामैटिक HTTP इंटरफेस अज्ञात उपयोगकर्ता एजेंटों को अस्वीकार करते हैं। एक अन्य संभावना है कि आपको बॉक्सकी को सर्वर पर वापस कुकीज़ प्रदान करने की नकल करने की आवश्यकता है। –

+0

हम्म, अच्छे अंक। मैंने उपयोगकर्ता एजेंट और कुकीज़ को अनदेखा कर दिया क्योंकि प्रक्रिया प्रक्रिया बिना ठीक काम करती है। इसे एक शॉट दे देंगे। धन्यवाद। –

+0

मैंने एक खाता खोलने और इसे स्वयं परीक्षण करने का प्रयास किया, लेकिन मुझे यह पता नहीं लगाया जा सकता कि boxee.tv पर सामान्य रूप से इस अनुरोध को ट्रिगर करने के लिए, इसलिए मैं इसे वायरशर्क में देख सकता हूं। –

उत्तर

0

यह मेरे लिए ठीक काम किया:

curl -v -A 'curl/7.16.3 (Windows build 7600; en-US; beta) boxee/0.9.21.11487' \ 
-H 'Content-Type: text/xml' -u "USER:PASS" \ 
--data '<message type="dequeue" referral="12573293"></message>' \ 
'http://app.boxee.tv/action/add' 

लेकिन मुझे 400 Bad Request मिलता है यदि मैं एक रेफ़रल आईडी को निकालने का प्रयास करता हूं जो वर्तमान में कतार में नहीं है। यदि आप वही रेफरल आईडी का उपयोग कर रहे हैं जैसा कि आपने वायरशर्क से पाया है, तो यह भी आपके लिए क्या हो रहा है, इसकी संभावना है।

wget -nv -m -nd --user=USER --password=PASS http://app.boxee.tv/api/get_queue 

यह सुनिश्चित करने के लिए कि आप जो निकालने का प्रयास कर रहे हैं वह वास्तव में कतार में है।