示例#1
0
def queryBooked( config, function, method, params, headers ):
  """Send a Booked REST API request

    Args:
      config(ConfigParser): Config file input data
      function(string): Name of Booked REST API function
      method(string): POST or GET
      params(string): Arguments to REST function
      headers(string): HTTP header info containing auth data

    Returns:
      dict: contains Booked auth info
      JSON object: response from server
  """
  connection = HTTPConnection( config.get("Server", "hostname") )
  connection.connect()

  if headers == None:
    creds = { 
        "username": config.get("Authentication", "username"), 
        "password": config.get("Authentication", "password") 
    }
    authUrl = config.get("Server", "baseUrl") + "Authentication/Authenticate"
    session = query( connection, authUrl, "POST", creds, {} )
    headers = { 
      "X-Booked-SessionToken": session['sessionToken'], 
      "X-Booked-UserId": session['userId']
    }

  url = config.get("Server", "baseUrl") + function 
  data = query( connection, url, method, params, headers )
  connection.close()
  return (headers, data)
示例#2
0
def urlopen(url, svprev, formdata):
    ua = "SPlayer Build %d" % svprev
    #prepare data
    #generate a random boundary
    boundary = "----------------------------" + "%x" % random.getrandbits(48)
    data = []
    for item in formdata:
        data.append("--" + boundary +
                    "\r\nContent-Disposition: form-data; name=\"" + item[0] +
                    "\"\r\n\r\n" + item[1] + "\r\n")
    data.append("--" + boundary + "--\r\n")
    data = "".join(data)
    cl = str(len(data))

    r = urlparse(url)
    h = HTTPConnection(r.hostname)
    h.connect()
    h.putrequest("POST", r.path, skip_host=True, skip_accept_encoding=True)
    h.putheader("User-Agent", ua)
    h.putheader("Host", r.hostname)
    h.putheader("Accept", "*/*")
    h.putheader("Content-Length", cl)
    h.putheader("Expect", "100-continue")
    h.putheader("Content-Type", "multipart/form-data; boundary=" + boundary)
    h.endheaders()

    h.send(data)

    resp = h.getresponse()
    if resp.status != OK:
        raise Exception("HTTP response " + str(resp.status) + ": " +
                        resp.reason)
    return resp
 def performRequestWithTimeOut(self, method = "HEAD", headers = {}, body = "", timeout = 3.0):
     """
     Perform a request with a timeout. 
     
     TODO: It would be nice if we could set timeouts on a per-socket basis. For the time being, it
     seems we have to operate with socket.setdefaulttimeout(), which is obviously a dangerous and
     annoying hack (since it applies to all future sockets). We need to make sure that in the end, 
     the default time out is set back to the original value (typically None). Since python2.4 does
     not yet support the finally clause, we have to do that in two places (i.e. in the try statement,
     if the request succeeds, in the catch statement otherwise).
     
     @see Clone.ping
     """
     if 'User-Agent' not in headers: headers['User-Agent'] = USER_AGENT # add default user agent
     conn = HTTPConnection(self.host, self.port)
     oldTimeOut = socket.getdefaulttimeout()
     socket.setdefaulttimeout(timeout)
     try:
         conn.connect()
         conn.request(
              method, 
              self.path,
              headers = headers,
              body = body
              )
         # revert back to blocking sockets -- 
         socket.setdefaulttimeout(oldTimeOut)
         return conn.getresponse()
     except:
         # revert back to blocking sockets
         socket.setdefaulttimeout(oldTimeOut)
         # then re-raise the exception
         raise
示例#4
0
def test_multiple_headers_concatenated_per_rfc_3875_section_4_1_18(dev_server):
    server = dev_server(r'''
    from werkzeug.wrappers import Response
    def app(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/plain')])
        return [environ['HTTP_XYZ'].encode()]
    ''')

    if sys.version_info[0] == 2:
        from httplib import HTTPConnection
    else:
        from http.client import HTTPConnection
    conn = HTTPConnection('127.0.0.1', server.port)
    conn.connect()
    conn.putrequest('GET', '/')
    conn.putheader('Accept', 'text/plain')
    conn.putheader('XYZ', ' a ')
    conn.putheader('X-INGNORE-1', 'Some nonsense')
    conn.putheader('XYZ', ' b')
    conn.putheader('X-INGNORE-2', 'Some nonsense')
    conn.putheader('XYZ', 'c ')
    conn.putheader('X-INGNORE-3', 'Some nonsense')
    conn.putheader('XYZ', 'd')
    conn.endheaders()
    conn.send(b'')
    res = conn.getresponse()

    assert res.status == 200
    assert res.read() == b'a ,b,c ,d'

    conn.close()
示例#5
0
    def connect(self):
        """
        Override the connect() function to intercept calls to certain
        host/ports.
        """
        if debuglevel:
            sys.stderr.write('connect: %s, %s\n' % (
                self.host,
                self.port,
            ))

        try:
            (app, script_name) = self.get_app(self.host, self.port)
            if app:
                if debuglevel:
                    sys.stderr.write('INTERCEPTING call to %s:%s\n' % \
                                     (self.host, self.port,))
                self.sock = wsgi_fake_socket(app, self.host, self.port,
                                             script_name)
            else:
                HTTPConnection.connect(self)

        except Exception, e:
            if debuglevel:  # intercept & print out tracebacks
                traceback.print_exc()
            raise
示例#6
0
    def _upload_file(self, data):
        if callable(data['make_snapshot_cb']):
            args = data['args'] if data['args'] is not None else []
            kwargs = data['kwargs'] if data['kwargs'] is not None else {}
            try:
                data['path'] = data['make_snapshot_cb'](*args, **kwargs)
            except NotImplementedError:
                return

        with open(data['path'], 'rb') as file_handle:
            url = data['url']
            parsed = urlparse(url)
            connection = HTTPConnection(parsed.netloc, timeout=10)
            try:
                connection.connect()
                request_url = parsed.path
                if parsed.query is not None and parsed.query != '':
                    request_url += '?'
                    request_url += parsed.query
                connection.request('POST', request_url, file_handle)
                resp = connection.getresponse()
                if resp.status >= 400:
                    logger.error('Failed to upload file: http error %s: %s',
                                 resp.status, resp.read())
            except Exception:
                logger.error('Failed to upload file: Network error %s',
                             error_str())
            finally:
                connection.close()
        if data.get('delete', False):
            os.unlink(data['path'])
示例#7
0
 def connect(self, url):
     try:
         connection = HTTPConnection(url)
         connection.connect()
         return connection
     except:
         raise Exception('Unable to connect to %r' % url)
示例#8
0
 def connect(self):
     """
     Override the connect() function to intercept calls to certain
     host/ports.
     
     If no app at host/port has been registered for interception then 
     a normal HTTPConnection is made.
     """
     if debuglevel:
         sys.stderr.write('connect: %s, %s\n' % (self.host, self.port,))
                          
     try:
         (app, script_name) = self.get_app(self.host, self.port)
         if app:
             if debuglevel:
                 sys.stderr.write('INTERCEPTING call to %s:%s\n' % \
                                  (self.host, self.port,))
             self.sock = wsgi_fake_socket(app, self.host, self.port,
                                          script_name)
         else:
             HTTPConnection.connect(self)
             
     except Exception, e:
         if debuglevel:              # intercept & print out tracebacks
             traceback.print_exc()
         raise
示例#9
0
def test_multiple_headers_concatenated_per_rfc_3875_section_4_1_18(dev_server):
    server = dev_server(r'''
    from werkzeug.wrappers import Response
    def app(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/plain')])
        return [environ['HTTP_XYZ'].encode()]
    ''')

    if sys.version_info[0] == 2:
        from httplib import HTTPConnection
    else:
        from http.client import HTTPConnection
    conn = HTTPConnection('127.0.0.1', server.port)
    conn.connect()
    conn.putrequest('GET', '/')
    conn.putheader('Accept', 'text/plain')
    conn.putheader('XYZ', ' a ')
    conn.putheader('X-INGNORE-1', 'Some nonsense')
    conn.putheader('XYZ', ' b')
    conn.putheader('X-INGNORE-2', 'Some nonsense')
    conn.putheader('XYZ', 'c ')
    conn.putheader('X-INGNORE-3', 'Some nonsense')
    conn.putheader('XYZ', 'd')
    conn.endheaders()
    conn.send(b'')
    res = conn.getresponse()

    assert res.status == 200
    assert res.read() == b'a ,b,c ,d'

    conn.close()
示例#10
0
    def test_get(self):

        PORT = TestMemHTTPServer.PORT

        server = MemHTTPServer(('localhost', PORT))
        server.set_get_output('asdf', 'text/html', 'ASDF')
        server.set_get_output('qwer', 'text/plain', 'QWER')

        server.server_activate()

        client = HTTPConnection('localhost', PORT)
        client.connect()
        client.request('GET', 'asdf')
        server.handle_request()
        response = client.getresponse()

        self.assertEqual(200, response.status)
        self.assertEqual('ASDF', response.read())
        self.assertEqual('text/html', response.getheader('Content-type'))

        client = HTTPConnection('localhost', PORT)
        client.connect()
        client.request('GET', 'qwer')
        server.handle_request()
        response = client.getresponse()

        self.assertEqual(200, response.status)
        self.assertEqual('QWER', response.read())
        self.assertEqual('text/plain', response.getheader('Content-type'))
示例#11
0
 def performRequestWithTimeOut(self,
                               method="HEAD",
                               headers={},
                               body="",
                               timeout=3.0):
     """
     Perform a request with a timeout. 
     
     TODO: It would be nice if we could set timeouts on a per-socket basis. For the time being, it
     seems we have to operate with socket.setdefaulttimeout(), which is obviously a dangerous and
     annoying hack (since it applies to all future sockets). We need to make sure that in the end, 
     the default time out is set back to the original value (typically None). Since python2.4 does
     not yet support the finally clause, we have to do that in two places (i.e. in the try statement,
     if the request succeeds, in the catch statement otherwise).
     
     @see Clone.ping
     """
     if 'User-Agent' not in headers:
         headers['User-Agent'] = USER_AGENT  # add default user agent
     conn = HTTPConnection(self.host, self.port)
     oldTimeOut = socket.getdefaulttimeout()
     socket.setdefaulttimeout(timeout)
     try:
         conn.connect()
         conn.request(method, self.path, headers=headers, body=body)
         # revert back to blocking sockets --
         socket.setdefaulttimeout(oldTimeOut)
         return conn.getresponse()
     except:
         # revert back to blocking sockets
         socket.setdefaulttimeout(oldTimeOut)
         # then re-raise the exception
         raise
    def _upload_file(self, data):
        if callable(data['make_snapshot_cb']):
            args = data['args'] if data['args'] is not None else []
            kwargs = data['kwargs'] if data['kwargs'] is not None else {}
            try:
                data['path'] = data['make_snapshot_cb'](*args, **kwargs)
            except NotImplementedError:
                return

        with open(data['path'], 'rb') as file_handle:
            url = data['url']
            parsed = urlparse(url)
            connection = HTTPConnection(parsed.netloc, timeout=10)
            try:
                connection.connect()
                request_url = parsed.path
                if parsed.query is not None and parsed.query != '':
                    request_url += '?'
                    request_url += parsed.query
                connection.request('POST', request_url, file_handle)
                resp = connection.getresponse()
                if resp.status >= 400:
                    logger.error('Failed to upload file: http error %s: %s', resp.status, resp.read())
            except Exception:
                logger.error('Failed to upload file: Network error %s', error_str())
            finally:
                connection.close()
        if data.get('delete', False):
            os.unlink(data['path'])
示例#13
0
def retrieveHttpResponse(url):
    host = getHostFromUrl(url)
    status, reason, responseText = None, None, None
    if None != host:
        conn = HTTPConnection(host)
    else:
        conn = HTTPConnection()
    conn.connect()
    try:
        conn.putrequest("GET", url)
        conn.putheader(
            "Accept",
            "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*")
        conn.putheader("Host", host)
        conn.putheader(
            "User-Agent",
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"
        )
        conn.putheader("Connection", "Keep-Alive")
        conn.endheaders()
        resp = conn.getresponse()
        status, reason, responseText = resp.status, resp.reason, resp.read()
    finally:
        conn.close()
    return status, reason, responseText
示例#14
0
def api(api_name, path):
    c = membase.incr(request.environ['REMOTE_ADDR'])
    if c == None:
        membase.set(request.environ['REMOTE_ADDR'], 1, 86400)
    elif c > 50000:
        abort(403)

    apis = {'silcc': 'www.opensilcc.com'}
    path = '/' + path

    if '?' in request.url:
        path += '?' + request.url.split('?')[1]

    api = HTTPConnection(apis[api_name], strict=True)
    api.connect()
    api.request(request.method, path, request.data)
    api_response = api.getresponse()
    api_response_content = api_response.read()
    api.close()

    gateway_response = make_response(api_response_content)
    gateway_response.status_code = api_response.status

    for header in ['Cache-Control', 'Content-Type', 'Pragma']:
        gateway_response.headers[header] = api_response.getheader(header)

    log_data = dict(api=api_name, path=path, data=request.data, response=api_response_content)
    log_entry = json.dumps(log_data)
    
    pika_channel.basic_publish(exchange='', routing_key='swiftgate', body=log_entry)

    return gateway_response
示例#15
0
def queryBooked(config, function, method, params, headers):
    """Send a Booked REST API request

    Args:
      config(ConfigParser): Config file input data
      function(string): Name of Booked REST API function
      method(string): POST or GET
      params(string): Arguments to REST function
      headers(string): HTTP header info containing auth data

    Returns:
      dict: contains Booked auth info
      JSON object: response from server
  """
    connection = HTTPConnection(config.get("Server", "hostname"))
    connection.connect()

    if headers == None:
        creds = {
            "username": config.get("Authentication", "username"),
            "password": config.get("Authentication", "password")
        }
        authUrl = config.get("Server",
                             "baseUrl") + "Authentication/Authenticate"
        session = query(connection, authUrl, "POST", creds, {})
        headers = {
            "X-Booked-SessionToken": session['sessionToken'],
            "X-Booked-UserId": session['userId']
        }

    url = config.get("Server", "baseUrl") + function
    data = query(connection, url, method, params, headers)
    connection.close()
    return (headers, data)
示例#16
0
 def login(self, username, password):
     connection = HttpConnection(self.flotrack_domain)
     connection.set_debuglevel(self.debug_level)
     connection.connect()
     # Configure login parameters (this is the content of the HTTP request)
     params = { "LoginForm[username]": username,
                "LoginForm[password]": password,
                "LoginForm[rememberMe]": 1, }
     encoded_params = url_encode(params)
     # Get the HTTP headers to use
     headers = self.get_default_headers(encoded_params)
     del headers["Cookie"]
     # Configure the cookie jar to store the login information
     cookie_request = HttpRequestAdapter(self.flotrack_domain, self.login_url,
                                         headers)
     self.cookie_jar.add_cookie_header(cookie_request)
     # Issue the HTTP request
     request = connection.request("POST", self.login_url, encoded_params, headers)
     response = connection.getresponse()
     if response.status == OK:
         return False
     if response.status == FOUND:
         response.read()
         response.info = lambda: response.msg
         # Extract the login cookies
         self.cookie_jar.extract_cookies(response, cookie_request)
         self.connection = connection
         return True
     raise Exception("Flotrack connection failed during login.")
示例#17
0
def urlopen(url, svprev, formdata):
    ua = "SPlayer Build %d" % svprev
    #prepare data
    #generate a random boundary
    boundary = "----------------------------" + "%x"%random.getrandbits(48)
    data = []
    for item in formdata:
        data.append("--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + item[0] + "\"\r\n\r\n" + item[1] + "\r\n")
    data.append("--" + boundary + "--\r\n")
    data = "".join(data)
    cl = str(len(data))

    r = urlparse(url)
    h = HTTPConnection(r.hostname)
    h.connect()
    h.putrequest("POST", r.path, skip_host=True, skip_accept_encoding=True)
    h.putheader("User-Agent", ua)
    h.putheader("Host", r.hostname)
    h.putheader("Accept", "*/*")
    h.putheader("Content-Length", cl)
    h.putheader("Expect", "100-continue")
    h.putheader("Content-Type", "multipart/form-data; boundary=" + boundary)
    h.endheaders()

    h.send(data)

    resp = h.getresponse()
    if resp.status != OK:
        raise Exception("HTTP response " + str(resp.status) + ": " + resp.reason)
    return resp
示例#18
0
 def wait_request(self, timeout=None):
     """ Blocks until the proxy successfully parses a request from atsev2 """
     try:
         connection = HTTPConnection(host=self._host, port=self._port)
         connection.connect()
     except Exception, e:
         raise BSPRemoteException("Can't connect to BrandServiceProxy at " +
                                  self.bsp_address() + ": " + str(e))
示例#19
0
 def get_config_str(self):
     """ Gets the current configuration as a string from BrandServiceProxy. """
     try:
         connection = HTTPConnection(host=self._host, port=self._port)
         connection.connect()
     except Exception, e:
         raise BSPRemoteException("Can't connect to BrandServiceProxy at " +
                                  self.bsp_address() + ": " + str(e))
 def connect(self, url):
     try:
         connection = HTTPConnection(
             url) if self.type == 'http' else HTTPSConnection(url)
         connection.connect()
         return connection
     except:
         raise Exception('Unable to connect to %r' % url)
 def connect(self, url):
     try:
         connection = HTTPConnection(
             url) if self.type == 'http' else HTTPSConnection(url)
         connection.connect()
         return connection
     except:
         raise Exception('Unable to connect to %r' % url)
示例#22
0
    def connect(self, url):
        try:
            conn = HTTPConnection(url)
            conn.connect()
            return conn

        except:
            print("Host not accessible at this time")
示例#23
0
 def connect(self):
     """Connect to the host and port specified in __init__ (through a
     proxy)."""
     # We are connecting to the proxy, so use the proxy settings
     self._set_hostport(self.__proxy, self.__proxy_port)
     HTTPConnection.connect(self)
     # Restore the real host and port
     self._set_hostport(self._host, self._port)
示例#24
0
    def connect(self):
        "Connect to a host on a given (SSL) port."
        if self.scheme == 'http':
            HTTPConnection.connect(self)
            return

        sock = _socket_create_connection((self.host, self.port), self.timeout)
        self.sock = _ssl_wrap_socket(sock, self.key_file, self.cert_file)
 def connect(self, url):
     try:
         connection = HTTPConnection(url)
         connection.set_debuglevel(self.http_debug)
         connection.connect()
         return connection
     except:
         raise Exception('Unable to connect to %r' % url)
示例#26
0
 def connect(self):
     """Connect to the host and port specified in __init__ (through a
     proxy)."""
     # We are connecting to the proxy, so use the proxy settings
     self._set_hostport(self.__proxy, self.__proxy_port)
     HTTPConnection.connect(self)
     # Restore the real host and port
     self._set_hostport(self._host, self._port)
示例#27
0
文件: https.py 项目: anutron/windmill
    def connect(self):
        "Connect to a host on a given (SSL) port."
        if self.scheme == 'http':
            HTTPConnection.connect(self)
            return

        sock = _socket_create_connection((self.host, self.port), self.timeout)
        self.sock = _ssl_wrap_socket(sock, self.key_file, self.cert_file)
示例#28
0
 def connect(self, url):
     try:
         connection = HTTPConnection(url)
         connection.set_debuglevel(self.http_debug)
         connection.connect()
         return connection
     except:
         error("Error connecting to '%s'" % url)
示例#29
0
 def connect(self, url):
     try:
         connection = HTTPConnection(url)
         connection.set_debuglevel(self.http_debug)
         connection.connect()
         return connection
     except:
         raise Exception('Unable to connect to %r' % url)
示例#30
0
 def connect(self, url):
     """ Make connection."""
     try:
         connection = HTTPConnection(url)
         connection.set_debuglevel(self.http_debug)
         connection.connect()
         return connection
     except:
         raise Exception("Error connecting to '%s'" % url)
示例#31
0
文件: cproxy.py 项目: chinple/mserver
    def __sendProxyRequest(self, reqObj, bbody):
        p = reqObj.path
        ip, port, host = self.__getProxyInfo__(reqObj.headers,
                                               reqObj.client_address, p)

        httpServ = HTTPConnection(ip, port)
        httpServ.connect()
        preqHeader = self.__getProxyHeader(reqObj.headers, host)
        httpServ.request(reqObj.command, p, bbody, preqHeader)
        return preqHeader, httpServ.getresponse()
示例#32
0
 def connect(self, url):
     print(self.try_counter)
     try:
         connection = HTTPConnection(url)
         connection.set_debuglevel(self.http_debug)
         connection.connect()
         return connection
     except:
         self.error_connect(url)
         self.try_counter = self.try_counter + 1
示例#33
0
 def unshorten(self, url):
     working = urlparse(url)
     if not working.netloc:
         raise TypeError, "Unable to parse URL."
     con = HTTPConnection(working.netloc)
     con.connect()
     con.request('GET', working.path)
     resp = con.getresponse()
     con.close()
     return resp.getheader('location')
示例#34
0
 def unshorten(self, url):
  working = urlparse(url)
  if not working.netloc:
   raise TypeError, "Unable to parse URL."
  con = HTTPConnection(working.netloc)
  con.connect()
  con.request('GET', working.path)
  resp = con.getresponse()
  con.close()
  return resp.getheader('location')
示例#35
0
 def connect(self):
     if not self.__ca_file:
         HTTPSConnection.connect(self)
     else:
         HTTPConnection.connect(self)
         if self.__ca_file == HTTPSConfigurableConnection.IGNORE:
             self.sock = ssl.wrap_socket(self.sock, cert_reqs=ssl.CERT_NONE)
         else:
             self.sock = ssl.wrap_socket(self.sock,
                                         ca_certs=self.__ca_file,
                                         cert_reqs=ssl.CERT_REQUIRED)
示例#36
0
    def connect(self):

        # proxy hack
        #self.host='127.0.0.1'
        #self.port=8888 #privoxy (def 8118)

        HTTPConnection.connect(self)
        try:
            self.sock.settimeout(30)
        except:
            pass
示例#37
0
def test_badpath_redirect(webapp):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    path = "/../../../../../../etc/passwd"

    connection.request("GET", path)
    response = connection.getresponse()
    assert response.status == 301
    assert response.reason == "Moved Permanently"

    connection.close()
示例#38
0
def test_badpath_redirect(webapp):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    path = "/../../../../../../etc/passwd"

    connection.request("GET", path)
    response = connection.getresponse()
    assert response.status == 301
    assert response.reason == "Moved Permanently"

    connection.close()
示例#39
0
 def do_query_specific_plaintext(self, host, http_port, method, route, payload = None, headers = {}):
     conn = HTTPConnection(host, http_port, timeout = 120)
     conn.connect()
     if payload is not None:
         conn.request(method, route, payload, headers)
     else:
         conn.request(method, route, "", headers)
     response = conn.getresponse()
     if response.status == 200:
         return response.read()
     else:
         raise BadServerResponse(response.status, response.reason)
示例#40
0
def test_response_body(webapp):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    connection.request("GET", "/response_body")
    response = connection.getresponse()
    assert response.status == 200
    assert response.reason == "OK"
    s = response.read()
    assert s == b("ä")

    connection.close()
示例#41
0
 def request(self, host, handler, request_body, verbose=0):
     from httplib import HTTPConnection
     con = HTTPConnection(self.__proxy, self.__port)
     con.connect()
     if verbose == 1 or self.verbose == 1:
         print "Server:\thttp://" + host + handler
         print "Sending:\t%s" % request_body
     con.request("POST", "http://" + host + handler, request_body)
     f = con.getresponse()
     self.verbose = verbose
     #print f.read()
     return (self.parse_response(f))
示例#42
0
 def do_query_specific_plaintext(self, host, http_port, method, route, payload = None, headers = {}):
     conn = HTTPConnection(host, http_port, timeout = 120)
     conn.connect()
     if payload is not None:
         conn.request(method, route, payload, headers)
     else:
         conn.request(method, route, "", headers)
     response = conn.getresponse()
     if response.status == 200:
         return response.read()
     else:
         raise BadServerResponse(response.status, response.reason)
示例#43
0
    def connect(self):
        "Connect to a host on a given (SSL) port."

        HTTPConnection.connect(self)

        if self._tunnel_host:
            server_hostname = self._tunnel_host
        else:
            server_hostname = self.host

        self.sock = self._context.wrap_socket(self.sock,
                                              server_hostname=server_hostname)
示例#44
0
def worker(cq, qcs, qns, cs, ns):
    #qcs : Queue Category list
    #qns : Queue Node list
    #cs : Categorey set s
    #ns : Node set s
    crt = currentThread()
    h = HTTPConnection("zh.wiktionary.org") #HTTP()
    h.connect()
    while not cq.empty():
        try:
            task = cq.get(timeout=5)
        except Queue.Empty:
            break

        print crt.getName(), task['url']
        h.putrequest('GET', task['url'])
        h.putheader("User-agent", "Mozilla/5.0")
        h.endheaders()
        
        with closing(h.getresponse()) as resp:
            f = resp.fp
            ret = f.read()

        cur_node = etree.HTML(ret)
        for node in cur_node.xpath("//div[@id='bodyContent']//a[starts-with(@href,'/wiki/Category:')]"):
            href, text = node.get("href"), node.text
            if "#" in href:
                href = href[:href.index("#")]

            if not href or href[:6] != "/wiki/":
                continue
            
            if href not in cs:
                #cts_will.append({"url": HOST + href, "alias": text})
                qcs.put({"url": "%s%s" % (HOST, href), "alias": text})
                cs[href] = [text]
            else:
                cs[href].append(text)
            
            #print node.get("href"), node.text

        for node in cur_node.xpath("//div[@id='bodyContent']//a[not(contains(@href,':'))]"):
            href, text = node.get("href"), node.text
            if "#" in href:
                href = href[:href.index("#")]
            if not href or href[:6] != "/wiki/":
                continue

            if href not in ns:
                qns.put({"url": "%s%s" % (HOST, href), "alias": text})
                ns[href] = [text]
            else:
                ns[href].append(text)
示例#45
0
def test_request_body(webapp, body):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    connection.request("GET", "/request_body", body)
    response = connection.getresponse()
    assert response.status == 200
    assert response.reason == "OK"
    s = response.read()
    assert s == body

    connection.close()
示例#46
0
    def connect(self):
        "Connect to a host on a given (SSL) port."

        HTTPConnection.connect(self)

        if self._tunnel_host:
            server_hostname = self._tunnel_host
        else:
            server_hostname = self.host

        self.sock = self._context.wrap_socket(self.sock,
                                              server_hostname=server_hostname)
示例#47
0
def test_response_body(webapp):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    connection.request("GET", "/response_body")
    response = connection.getresponse()
    assert response.status == 200
    assert response.reason == "OK"
    s = response.read()
    assert s == u"ä".encode('utf-8')

    connection.close()
示例#48
0
def test(webapp, method):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    connection.request(method, "/")
    response = connection.getresponse()
    assert response.status == 200
    assert response.reason == "OK"

    s = response.read()
    assert s == "{0:s}".format(method).encode("utf-8")

    connection.close()
示例#49
0
def test(webapp, method):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    connection.request(method, "/")
    response = connection.getresponse()
    assert response.status == 200
    assert response.reason == "OK"

    s = response.read()
    assert s == "{0:s}".format(method).encode("utf-8")

    connection.close()
示例#50
0
 def set_config(self, config):
     """ Sets the configuration (it can be a string or a SafeConfigParser
     object with all the configurations added as optuins in sections per plugin """
     if isinstance(config, SafeConfigParser):
         buf = StringIO()
         config.write(buf)
         config = buf.getvalue()
     try:
         connection = HTTPConnection(host=self._host, port=self._port)
         connection.connect()
     except Exception, e:
         raise BSPRemoteException("Can't connect to BrandServiceProxy at " +
                                  self.bsp_address() + ": " + str(e))
示例#51
0
def test_argument(webapp):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    data = 'arg=%E2%86%92'
    connection.request("POST", "/argument", data, {"Content-type": "application/x-www-form-urlencoded"})
    response = connection.getresponse()
    assert response.status == 200
    assert response.reason == "OK"
    s = response.read()
    assert s.decode('utf-8') == u'\u2192'

    connection.close()
示例#52
0
def makeRequest(url, values=None, verb='GET', accept="text/plain",
                contentType = None, secure = False, secureParam = {}):
    headers = {}
    contentType = contentType or "application/x-www-form-urlencoded"
    headers = {"content-type": contentType,
               "Accept": accept,
               "cms-auth-status": "NONE"}
    if secure:
        headers.update({"cms-auth-status": "OK",
                        "cms-authn-dn": "/DC=ch/OU=Organic Units/OU=Users/CN=Fake User",
                        "cms-authn-name": "Fake User",
                        "cms-authz-%s" % secureParam['role']:
                                   "group:%s site:%s" %(secureParam['group'],
                                                        secureParam['site'])})
        headers["cms-authn-hmac"] = _generateHash(secureParam["key"], headers)

    data = None
    if verb == 'GET' and values:
        data = urllib.urlencode(values, doseq=True)
    elif verb != 'GET' and values:
        # needs to test other encoding type
        if contentType == "application/x-www-form-urlencoded":
            data = urllib.urlencode(values)
        else:
            # for other encoding scheme values assumed to be encoded already
            data = values
    parser = urlparse(url)
    uri = parser.path
    if parser.query:
        uri += "?" + parser.query

    if verb == 'GET' and data != None:
        uri = '%s?%s' % (uri, data)

    # need to specify Content-length for POST method
    # TODO: this function needs refactoring - too verb-related branching
    if verb != 'GET':
        if data:
            headers.update({"content-length": len(data)})
        else:
            headers.update({"content-length" : 0})

    conn = HTTPConnection(parser.netloc)
    conn.connect()
    conn.request(verb, uri, data, headers)
    response = conn.getresponse()

    data = response.read()
    conn.close()
    cType = response.getheader('content-type').split(';')[0]
    return data, response.status, cType, response
示例#53
0
def test_bad_header(webapp):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    connection.putrequest("GET", "/", "HTTP/1.1")
    connection.putheader("Connection", "close")
    connection._output(b("X-Foo"))  # Bad Header
    connection.endheaders()

    response = connection.getresponse()
    assert response.status == 400
    assert response.reason == "Bad Request"

    connection.close()
示例#54
0
def test(webapp):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.auto_open = False
    connection.connect()

    for i in range(2):
        connection.request("GET", "/")
        response = connection.getresponse()
        assert response.status == 200
        assert response.reason == "OK"
        s = response.read()
        assert s == b"Hello World!"

    connection.close()
示例#55
0
def test_bad_header(webapp):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    connection.putrequest("GET", "/", "HTTP/1.1")
    connection.putheader("Connection", "close")
    connection._output(b("X-Foo"))  # Bad Header
    connection.endheaders()

    response = connection.getresponse()
    assert response.status == 400
    assert response.reason == "Bad Request"

    connection.close()
示例#56
0
def test_request_headers(webapp):
    connection = HTTPConnection(webapp.server.host, webapp.server.port)
    connection.connect()

    body = b("")
    headers = {"A": "ä"}
    connection.request("GET", "/request_headers", body, headers)
    response = connection.getresponse()
    assert response.status == 200
    assert response.reason == "OK"
    s = response.read()
    assert s == b("ä")

    connection.close()
示例#57
0
def file_to_url(method,
                url,
                file,
                content_type='application/octet-stream',
                user_token=None,
                product_token=None):
    hash = md5.new()
    length = 0

    while True:
        block = file.read(4096)
        if not block:
            break
        length += len(block)
        hash.update(block)

    headers = devpay_headers(user_token, product_token)
    headers = rest_headers(method, url, hash, content_type, headers)

    file.seek(0)

    #print 'Content-Length:', str(length)
    headers['Content-Length'] = str(length)

    c = HTTPConnection(REST_HOST)
    #c.set_debuglevel(9)
    c.connect()
    c.putrequest(method, url)
    for key, value in headers.items():
        c.putheader(key, value)
    c.endheaders()

    while length > 4096:
        block = file.read(4096)
        if not block:
            raise "Unexpected EOF"

        c.send(block)
        sys.stdout.write('.')
        sys.stdout.flush()
        length -= len(block)

    while length > 0:
        block = file.read(length)
        if not block:
            raise "Unexpected EOF"
        c.send(block)
        length -= len(block)

    return c.getresponse()
示例#58
0
    def test_404(self):

        PORT = TestMemHTTPServer.PORT

        server = MemHTTPServer(('localhost', PORT))

        server.server_activate()

        client = HTTPConnection('localhost', PORT)
        client.connect()
        client.request('GET', 'asdf')
        server.handle_request()
        response = client.getresponse()

        self.assertEqual(404, response.status)
示例#59
0
	def get_response(self, http_method, request_url, request_body, request_header):
		http_connection = HTTPConnection(self.domain, self.port, _DEFAULT_TAOBAO_TOP_TIMEOUT)
		http_connection.connect()
		http_connection.request(http_method, request_url, body = request_body, headers = request_header)
		http_response = http_connection.getresponse()

		if http_response.status is not 200:
			raise RequestException('invalid http status ' + str(http_connection.status) + ', detail body :' + http_response.read())
		http_request_data = http_response.read()

		json_data_obj = json.loads(http_request_data)
		if 'error_response' in json_data_obj:
			#todo 详细的错误
			return json_data_obj

		return json_data_obj