requests.exceptions.SSLError:[SSL:SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3警报握手失败(_ssl.c:590)

时间:2016-10-08 20:56:54

标签: python ssl handshake sslv3

我有一个用Python编写的脚本,如下所示

#!/bin/env python2.7

# Run around 1059 as early as 1055.
# Polling times vary pick something nice.
# Ghost checkout timer can be changed by 
# adjusting for loop range near bottom.
# Fill out personal data in checkout payload dict.

import sys, json, time, requests, urllib2
from datetime import datetime

qty='1'

def UTCtoEST():
    current=datetime.now()
    return str(current) + ' EST'
print
poll=raw_input("Polling interval? ")
poll=int(poll)
keyword=raw_input("Product name? ").title()       # hardwire here by declaring keyword as a string 
color=raw_input("Color? ").title()                # hardwire here by declaring keyword as a string
sz=raw_input("Size? ").title()                    # hardwire here by declaring keyword as a string
print 
print UTCtoEST(),':: Parsing page...'
def main():
    global ID
    global variant
    global cw
    req = urllib2.Request('http://www.supremenewyork.com/mobile_stock.json')
    req.add_header('User-Agent', "User-Agent','Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B350 Safari/8536.25")
    resp = urllib2.urlopen(req)
    data = json.loads(resp.read())
    ID=0
    for i in range(len(data[u'products_and_categories'].values())):
        for j in range(len(data[u'products_and_categories'].values()[i])):
            item=data[u'products_and_categories'].values()[i][j]
            name=str(item[u'name'].encode('ascii','ignore'))
            # SEARCH WORDS HERE
            # if string1 in name or string2 in name:
            if keyword in name:
                # match/(es) detected!
                # can return multiple matches but you're 
                # probably buying for resell so it doesn't matter
                myproduct=name                
                ID=str(item[u'id'])
                print UTCtoEST(),'::',name, ID, 'found ( MATCHING ITEM DETECTED )'
    if (ID == 0):
        # variant flag unchanged - nothing found - rerun
        time.sleep(poll)
        print UTCtoEST(),':: Reloading and reparsing page...'
        main()
    else:
        print UTCtoEST(),':: Selecting',str(myproduct),'(',str(ID),')'
        jsonurl = 'http://www.supremenewyork.com/shop/'+str(ID)+'.json'
        req = urllib2.Request(jsonurl)
        req.add_header('User-Agent', "User-Agent','Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B350 Safari/8536.25")
        resp = urllib2.urlopen(req)
        data = json.loads(resp.read())
        found=0
        for numCW in data['styles']:
            # COLORWAY TERMS HERE
            # if string1 in numCW['name'] or string2 in numCW['name']:
            if color in numCW['name'].title():
                for sizes in numCW['sizes']:
                    # SIZE TERMS HERE
                    if str(sizes['name'].title()) == sz: # Medium
                        found=1;
                        variant=str(sizes['id'])
                        cw=numCW['name']
                        print UTCtoEST(),':: Selecting size:', sizes['name'],'(',numCW['name'],')','(',str(sizes['id']),')'

        if found ==0:
            # DEFAULT CASE NEEDED HERE - EITHER COLORWAY NOT FOUND OR SIZE NOT IN RUN OF PRODUCT
            # PICKING FIRST COLORWAY AND LAST SIZE OPTION
            print UTCtoEST(),':: Selecting default colorway:',data['styles'][0]['name']
            sizeName=str(data['styles'][0]['sizes'][len(data['styles'][0]['sizes'])-1]['name'])
            variant=str(data['styles'][0]['sizes'][len(data['styles'][0]['sizes'])-1]['id'])
            cw=data['styles'][0]['name']
            print UTCtoEST(),':: Selecting default size:',sizeName,'(',variant,')'
main()


session=requests.Session()
addUrl='http://www.supremenewyork.com/shop/'+str(ID)+'/add.json'
addHeaders={
    'Host':              'www.supremenewyork.com',                                                                                                                     
    'Accept':            'application/json',                                                                                                                             
    'Proxy-Connection':  'keep-alive',                                                                                                                                   
    'X-Requested-With':  'XMLHttpRequest',                                                                                                                               
    'Accept-Encoding':   'gzip, deflate',                                                                                                                                
    'Accept-Language':   'en-us',                                                                                                                                        
    'Content-Type':      'application/x-www-form-urlencoded',                                                                                                            
    'Origin':            'http://www.supremenewyork.com',                                                                                                                
    'Connection':        'keep-alive',                                                                                                                                   
    'User-Agent':        'Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257',                               
    'Referer':           'http://www.supremenewyork.com/mobile'   
}
addPayload={
    'size': str(variant),
    'qty':  '1'
}
print UTCtoEST() +' :: Adding product to cart...'
addResp=session.post(addUrl,data=addPayload,headers=addHeaders)

print UTCtoEST() +' :: Checking status code of response...'

if addResp.status_code!=200:
    print UTCtoEST() +' ::',addResp.status_code,'Error \nExiting...'
    print
    sys.exit()
else:
    if addResp.json()==[]:
        print UTCtoEST() +' :: Response Empty! - Problem Adding to Cart\nExiting...'
        print
        sys.exit()
    print UTCtoEST() +' :: '+str(cw)+' - '+addResp.json()[0]['name']+' - '+ addResp.json()[0]['size_name']+' added to cart!'

    checkoutUrl='https://www.supremenewyork.com/checkout.json'
    checkoutHeaders={
        'host':              'www.supremenewyork.com',
        'If-None-Match':    '"*"',
        'Accept':            'application/json',                                                                                                                             
        'Proxy-Connection':  'keep-alive',                                                                                                                                   
        'Accept-Encoding':   'gzip, deflate',                                                                                                                                
        'Accept-Language':   'en-us',                                                                                                                                        
        'Content-Type':      'application/x-www-form-urlencoded',                                                                                                            
        'Origin':            'http://www.supremenewyork.com',                                                                                                                
        'Connection':        'keep-alive',                                                                                                                                   
        'User-Agent':        'Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257',                               
        'Referer':           'http://www.supremenewyork.com/mobile'   
    }

    #################################
    # FILL OUT THESE FIELDS AS NEEDED
    #################################
    checkoutPayload={
        'store_credit_id':    '',      
        'from_mobile':              '1',
        'cookie-sub':               '%7B%22'+str(variant)+'%22%3A1%7D',       # cookie-sub: eg. {"VARIANT":1} urlencoded
        'same_as_billing_address':  '1',                                    
        'order[billing_name]':      'anon mous',                              # FirstName LastName
        'order[email]':             'anon@mailinator.com',                    # email@domain.com
        'order[tel]':               '999-999-9999',                           # phone-number-here
        'order[billing_address]':   '123 Seurat lane',                        # your address
        'order[billing_address_2]': '',
        'order[billing_zip]':       '90210',                                  # zip code
        'order[billing_city]':      'Beverly Hills',                          # city
        'order[billing_state]':     'CA',                                     # state
        'order[billing_country]':   'USA',                                    # country
        'store_address':            '1',                                
        'credit_card[type]':        'visa',                                   # master or visa
        'credit_card[cnb]':         '9999 9999 9999 9999',                    # credit card number
        'credit_card[month]':       '01',                                     # expiration month
        'credit_card[year]':        '2026',                                   # expiration year
        'credit_card[vval]':        '123',                                    # cvc/cvv
        'order[terms]':             '0',
        'order[terms]':             '1'                
    }

    # GHOST CHECKOUT PREVENTION WITH ROLLING PRINT
    for i in range(5):
            sys.stdout.write("\r" +UTCtoEST()+ ' :: Sleeping for '+str(5-i)+' seconds to avoid ghost checkout...')
            sys.stdout.flush()
            time.sleep(1)
    print 
    print UTCtoEST()+ ' :: Firing checkout request!'
    checkoutResp=session.post(checkoutUrl,data=checkoutPayload,headers=checkoutHeaders)
    try:
        print UTCtoEST()+ ' :: Checkout',checkoutResp.json()['status'].title()+'!'
    except:
        print UTCtoEST()+':: Error reading status key of response!'
        print checkoutResp.json()
    print 
    print checkoutResp.json()
    if checkoutResp.json()['status']=='failed':
        print
        print '!!!ERROR!!! ::',checkoutResp.json()['errors']
    print

当我想要运行它时,一切都正常,但最后它给了我这个错误:

Traceback (most recent call last):


File "/Users/"USERNAME"/Desktop/supreme.py", line 167, in <module>
    checkoutResp=session.post(checkoutUrl,data=checkoutPayload,headers=checkoutHeaders)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 522, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 596, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/adapters.py", line 497, in send
    raise SSLError(e, request=request)
SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:590)

1 个答案:

答案 0 :(得分:0)

the SSLLabs report可以看出,服务器仅支持TLS 1.2并且仅支持非常少的密码。假设您的错误输出中有/Users/...这样的路径,我假设您使用的是Mac OS X.Mac OS X附带的可能由您的Python使用的OpenSSL版本是0.9.8,这个版本太旧而无法支持TLS 1.2。这意味着服务器将不接受来自客户端的SSL 3.0或TLS 1.0请求。

有关如何通过更新OpenSSL版本来解决此问题的信息,请参阅Updating openssl in python 2.7