(self, data, auth=None)
return result
Post a query to the server, and return a string response.
Post a query to the server, and return a string response.
Post a query to the server and return a string response .
def post_to_server(self, data, auth=None):
        ''' Post a query to the server, and return a string response.
        '''
        if 'name' in data:
            self.announce('Registering %s to %s' % (data['name'],
                                                   self.repository),
                                                   log.INFO)
        # Build up the MIME payload for the urllib2 POST data
        boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
        sep_boundary = '\n--' + boundary
        end_boundary = sep_boundary + '--'
        body = StringIO.StringIO()
        for key, value in data.items():
            # handle multiple entries for the same name
            if type(value) not in (type([]), type( () )):
                value = [value]
            for value in value:
                body.write(sep_boundary)
                body.write('\nContent-Disposition: form-data; name="%s"'%key)
                body.write("\n\n")
                body.write(value)
                if value and value[-1] == '\r':
                    body.write('\n')  # write an extra newline (lurve Macs)
        body.write(end_boundary)
        body.write("\n")
        body = body.getvalue()

        # build the Request
        headers = {
            'Content-type': 'multipart/form-data; boundary=%s; charset=utf-8'%boundary,
            'Content-length': str(len(body))
        }
        req = urllib2.Request(self.repository, body, headers)

        # handle HTTP and include the Basic Auth handler
        opener = urllib2.build_opener(
            urllib2.HTTPBasicAuthHandler(password_mgr=auth)
        )
        data = ''
        try:
            result = opener.open(req)
        except urllib2.HTTPError, e:
            if self.show_response:
                data = e.fp.read()
            result = e.code, e.msg
        except urllib2.URLError, e:
            result = 500, str(e)
        else:
            if self.show_response:
                data = result.read()
            result = 200, 'OK'
        if self.show_response:
            dashes = '-' * 75
            self.announce('%s%s%s' % (dashes, data, dashes))

        return result
def post_to_server ( self , data , auth = None ) : if 'name' in data : self . announce ( 'Registering %s to %s' % ( data [ 'name' ] , self . repository ) , log . INFO ) # Build up the MIME payload for the urllib2 POST data boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' sep_boundary = '\n--' + boundary end_boundary = sep_boundary + '--' body = StringIO . StringIO ( ) for key , value in data . items ( ) : # handle multiple entries for the same name if type ( value ) not in ( type ( [ ] ) , type ( ( ) ) ) : value = [ value ] for value in value : body . write ( sep_boundary ) body . write ( '\nContent-Disposition: form-data; name="%s"' % key ) body . write ( "\n\n" ) body . write ( value ) if value and value [ - 1 ] == '\r' : body . write ( '\n' ) # write an extra newline (lurve Macs) body . write ( end_boundary ) body . write ( "\n" ) body = body . getvalue ( ) # build the Request headers = { 'Content-type' : 'multipart/form-data; boundary=%s; charset=utf-8' % boundary , 'Content-length' : str ( len ( body ) ) } req = urllib2 . Request ( self . repository , body , headers ) # handle HTTP and include the Basic Auth handler opener = urllib2 . build_opener ( urllib2 . HTTPBasicAuthHandler ( password_mgr = auth ) ) data = '' try : result = opener . open ( req ) except urllib2 . HTTPError , e : if self . show_response : data = e . fp . read ( ) result = e . code , e . msg except urllib2 . URLError , e : result = 500 , str ( e ) else : if self . show_response : data = result . read ( ) result = 200 , 'OK' if self . show_response : dashes = '-' * 75 self . announce ( '%s%s%s' % ( dashes , data , dashes ) ) return result