Пример #1
0
    def post(self, filename, ccd_data, use_base64=False):
        """ filename   : The name of the file attachment
            ccd_data   : The xml to post (send)
            use_base64 : If true, encode the encrypted XML as base64. Maybe necessary for 3DES
        """
        attachments = ""
        payload = """--%s
Content-Disposition: attachment; name="%s"; filename="%s.xml"
Content-Type: text/xml

%s"""

        payload_uuid = str(uuid.uuid4())
        try:
            for data in ccd_data:
                if len(attachments) > 0:
                    attachments += "\r\n\r\n"
                if use_base64:
                    attachments += payload % (payload_uuid, filename, filename, base64.b64encode(data))
                else:
                    attachments += payload % (payload_uuid, filename, filename, data)
            #import pdb; pdb.set_trace()
            if self._url.find("https") == 0:
                https = urllib.request.HTTPSHandler(debuglevel=1)
                opener = urllib.request.build_opener(https)
                urllib.request.install_opener(opener)
                request = urllib.request.Request(self._url)
                request.add_header("Content-Type", "multipart/form-data; boundary=%s" % payload_uuid)
                request.add_header("User-Agent", "synthesis")
                request.add_data(attachments)
                if PRINT_HTTP_POST:
                    print(request.header_items())
                    print(request.get_data())
                    print(request.get_method())
                    print(request.get_host())
                    return (True, "True")
                response = urllib2.urlopen(request).read()
            else:
                print("**** POSTING TO LOCAL SERVER ****") 
                request = urllib.request.Request(self._url)
                request.add_header("Content-Type", "multipart/form-data; boundary=%s" % payload_uuid)
                request.add_header("User-Agent", "synthesis")
                request.add_data(attachments)
                if PRINT_HTTP_POST:
                    print(request.header_items())
                    print(request.get_data())
                    print(request.get_method())
                    print(request.get_host())
                    return (True, "True")
                response = urllib2.urlopen(request).read()

            # check for some sign of success within the response
            if response[0:4] == "202:":
                return (True, response)
            else:
                return (False, response)
        except Exception as err:
            return (False, "An error occurred while performing an HTTP-POST or receiving the response: (%s)" % str(err))
Пример #2
0
 def test_custom_headers(self):
     url = "http://www.example.com"
     with support.transient_internet(url):
         opener = urllib.request.build_opener()
         request = urllib.request.Request(url)
         self.assertFalse(request.header_items())
         opener.open(request)
         self.assertTrue(request.header_items())
         self.assertTrue(request.has_header('User-agent'))
         request.add_header('User-Agent','Test-Agent')
         opener.open(request)
         self.assertEqual(request.get_header('User-agent'),'Test-Agent')
Пример #3
0
 def test_custom_headers(self):
     url = support.TEST_HTTP_URL
     with support.transient_internet(url):
         opener = urllib.request.build_opener()
         request = urllib.request.Request(url)
         self.assertFalse(request.header_items())
         opener.open(request)
         self.assertTrue(request.header_items())
         self.assertTrue(request.has_header('User-agent'))
         request.add_header('User-Agent', 'Test-Agent')
         opener.open(request)
         self.assertEqual(request.get_header('User-agent'), 'Test-Agent')
Пример #4
0
def UpdateTaskTW(task_id, task_name, start_date, end_date):
    company = "project36"
    key = base64.b64encode(b'twp_TRKx81UCnv4deufBFU2b85350cXo:xxx')
    task_id = "6183721"

    task_name = task_name
    start_date = start_date
    due_date = end_date

    root = ET.Element("request")
    todo_el = ET.SubElement(root, "todo-item")
    content_el = ET.SubElement(todo_el, "content")
    id_el = ET.SubElement(todo_el, "id")
    start_date_el = ET.SubElement(todo_el, "start-date")
    due_date_el = ET.SubElement(todo_el, "due-date")

    content_el.text = task_name
    start_date_el.text = start_date
    due_date_el.text = due_date
    id_el.text = task_id

    json_string = ET.tostring(root, encoding="utf-8", method="xml")
    request = urllib.request.Request(
        "https://{0}.teamwork.com/tasks/{1}.json".format(company, task_id),
        json_string)
    print(request)
    request.add_header("Authorization", "BASIC " + key.decode())
    request.add_header("Content-type", "application/json")
    request.get_method = lambda: "PUT"
    print(request.header_items())
    response = urllib.request.urlopen(request)
    data = response.read()
    print(data)
Пример #5
0
def AddTask(tasklist_id, task_name, start_date, end_date):
    company = "project36"
    key = base64.b64encode(b'twp_TRKx81UCnv4deufBFU2b85350cXo:xxx')
    tasklist_id = tasklist_id
    task_name = task_name
    start_date = start_date
    due_date = end_date

    root = ET.Element("request")
    todo_el = ET.SubElement(root, "todo-item")
    content_el = ET.SubElement(todo_el, "content")
    start_date_el = ET.SubElement(todo_el, "start-date")
    due_date_el = ET.SubElement(todo_el, "due-date")

    content_el.text = task_name
    start_date_el.text = start_date
    due_date_el.text = due_date

    json_string = ET.tostring(root, encoding="utf-8", method="xml")
    request = urllib.request.Request(
        "https://{0}.teamwork.com/todo_lists/{1}/todo_items.json".format(
            company, tasklist_id), json_string)

    request.add_header("Authorization", "BASIC " + key.decode())
    request.add_header("Content-type", "application/json")
    print(request.header_items())

    response = urllib.request.urlopen(request)
    data = response.read()
    print(data)
    x = ast.literal_eval(data.decode())

    return x['id']
Пример #6
0
 def start_request(self, request):
     with self._setup_request(request):
         self._check_reusable()
         self._connection.putrequest(
             request.get_method(), request.selector)
         for item in request.header_items():
             self._connection.putheader(*item)
         self._connection.endheaders()
Пример #7
0
    def http_request(self, request):
        host, full_url = request.host, request.get_full_url()
        url_path = full_url[full_url.find(host) + len(host):]
        log_url(self.log, "Requesting: ", request.get_full_url(), TRACE_LEVEL)
        self.log.log(self.log_level,
                     "%s %s" % (request.get_method(), url_path))

        for header in request.header_items():
            self.log.log(self.log_level, " . %s: %s" % header[:])

        return request
Пример #8
0
 def copyFile(token, target_name):
     print("Access Token: " + token)
     url_destino = "https://www.googleapis.com/drive/v2/files/0AilPd9i9ydNTdFc4a2lvYmZnNkNzSU1kdVFZb0syN1E/copy" \
                   "?key=(YOUR_API_KEY provided by Google API Console)"
     values = "{'title': '%s'}" % target_name
     data = values.encode('utf-8')
     request = urllib.request.Request(url_destino, data, method='POST')
     request.add_header("Authorization", "Bearer " + token)
     request.add_header("Content-Length", len(data))
     request.add_header("Content-Type", "application/json")
     print(request.header_items())
     f = urllib.request.urlopen(request)
     print(f.read())
Пример #9
0
    def http_request(self, request):
        if __debug__:
            host, full_url = request.get_host(), request.get_full_url()
            url_path = full_url[full_url.find(host) + len(host):]
            self.httpout.write("%s\n" % request.get_full_url())
            self.httpout.write("\n")
            self.httpout.write("%s %s\n" % (request.get_method(), url_path))

            for header in request.header_items():
                self.httpout.write("%s: %s\n" % header[:])

            self.httpout.write("\n")

        return request
Пример #10
0
def DeleteProjectTW(project_id):
    company = "project36"
    key = base64.b64encode(b'twp_TRKx81UCnv4deufBFU2b85350cXo:xxx')
    project_id = project_id
    request = urllib.request.Request(
        "https://{0}.teamwork.com/projects/{1}.json".format(company, project_id))
    print(request)
    request.add_header("Authorization", "BASIC " + key.decode())
    request.add_header("Content-type", "application/json")
    request.get_method = lambda: "DELETE"
    print(request.header_items())
    response = urllib.request.urlopen(request)

    data = response.read()
    print(request.get_method())
    print(data)
def chrome_user_agent():
    opener = urllib.request.build_opener()
    opener.addheaders = [('User-agent', USER_AGENT)]
    urllib.request.install_opener(opener)
    response = urllib.request.urlopen(url)
    print("Response headers")
    print("--------------------")
    for header, value in response.getheaders():
        print(header + ":" + value)

    request = Request(url)
    request.add_header('User-agent', USER_AGENT)
    print("\nRequest headers")
    print("--------------------")
    for header, value in request.header_items():
        print(header + ":" + value)
Пример #12
0
def log_curl_request(request, cookie_jar):
    if not os.getenv("CURL_CMD"):
        return
    cmd = []
    cmd += "curl -v"
    if request.get_method() == "GET":
        # No verb to add here
        pass
    headers = request.header_items()
    for header_name, header_val in headers:
        cmd += ' -H "%s: %s"' % (header_name, header_val)
    url = " " + request.full_url
    escaped_url = ""
    for char in url:
        if char == "&":
            escaped_url += "\\"
        escaped_url += char
    cmd += escaped_url
    print("".join(cmd))
Пример #13
0
def UpdateProjectTW(project_id, project_name, start_date, end_date, description ):
    company = "project36"
    key = base64.b64encode(b'twp_TRKx81UCnv4deufBFU2b85350cXo:xxx')
    projectt_id = project_id
    print(project_id)
    project_name = project_name
    project_description = description
    start_date = start_date
    due_date = end_date

    root = ET.Element("request")
    todo_el = ET.SubElement(root, "project")
    content_el = ET.SubElement(todo_el, "name")
    description_el = ET.SubElement(todo_el, "description")
    start_date_el = ET.SubElement(todo_el, "startDate")
    end_date_el = ET.SubElement(todo_el, "endDate")

    content_el.text = project_name
    description_el.text = project_description
    start_date_el.text = start_date
    end_date_el.text = due_date

    json_string = ET.tostring(root, encoding="utf-8", method="xml")
    print("**********+json-string**************")
    print(json_string.decode())

    request = urllib.request.Request(
        "https://{0}.teamwork.com/projects/{1}.json".format(company, projectt_id),
        json_string)
    print(request)
    request.add_header("Authorization", "BASIC " + key.decode())
    request.add_header("Content-type", "application/json")
    request.get_method = lambda: "PUT"
    print(request.header_items())
    response = urllib.request.urlopen(request)

    data = response.read()
    print(request.get_method())
    print(data)
Пример #14
0
def add_project(name_project, description_project, start_date, end_date):
    company = "project36"
    key = base64.b64encode(b'twp_TRKx81UCnv4deufBFU2b85350cXo:xxx')
    project_name = name_project
    project_description =  description_project
    start_date = start_date
    due_date = end_date

    root = ET.Element("request")
    todo_el = ET.SubElement(root, "project")
    content_el = ET.SubElement(todo_el, "name")
    description_el = ET.SubElement(todo_el, "description")
    start_date_el = ET.SubElement(todo_el, "startDate")
    due_date_el = ET.SubElement(todo_el, "endDate")

    content_el.text = project_name
    description_el.text = project_description
    start_date_el.text = start_date
    due_date_el.text = due_date

    action = "projects.json"

    json_string = ET.tostring(root, encoding="utf-8", method="xml")

    print(json_string)

    request = urllib.request.Request(
        "https://{0}.teamwork.com/{1}".format(company, action),
        json_string)
    print(request)
    request.add_header("Authorization", "BASIC " + key.decode())
    request.add_header("Content-type", "application/xml")
    print(request.header_items())
    response = urllib.request.urlopen(request)
    data = response.read()
    #Convierte la data en un diccionario
    x = ast.literal_eval(data.decode())

    return x['id']
Пример #15
0
def askPage(opener, url="https://api.api.ai/v1/query?v=20170712", data=None, headers=None, method='GET'):
    print("-- in askPage() --")
    request = urllib.request.Request(url, method=method)
    if headers is not None:
        for key in headers:
            request.add_header(key, headers[key])

    print("method: ")
    pprint(request.get_method())
    print("url: ")
    pprint(request.get_full_url())
    print("items: ")
    pprint(request.header_items())
    print(" ")

    try:
        result = opener.open(request, data)
    except IOError as e:
        print("!!!! ERROR!!!")
        print (e)

    return result
Пример #16
0
def NewTaskList(tasklist_name, project_id, project_name):

    company = "project36"
    key = base64.b64encode(b'twp_TRKx81UCnv4deufBFU2b85350cXo:xxx')

    project_id = project_id
    tasklist_name = tasklist_name
    project_name = project_name

    root = ET.Element("request")
    task = ET.SubElement(root, "todo-list")
    name_el = ET.SubElement(task, "name")

    projectName_el = ET.SubElement(task, "projectname")
    content_el = ET.SubElement(task, "projectid")

    name_el.text = tasklist_name
    content_el.text = project_id
    projectName_el.text = project_name

    json_string = ET.tostring(root, encoding="utf-8", method="xml")
    print(json_string)

    request = urllib.request.Request(
        "https://{0}.teamwork.com/projects/{1}/tasklists.json".format(
            company, project_id), json_string)
    print("request**")
    print(request)

    request.add_header("Authorization", "BASIC " + key.decode())
    request.add_header("Content-type", "application/json")
    print(request.header_items())
    response = urllib.request.urlopen(request)
    data = response.read()
    print(data)
    x = ast.literal_eval(data.decode())

    return x['TASKLISTID']
Пример #17
0
    def __write_capture(self, request, response):

        ohandle = io.StringIO()
        response_body = b''
        saved_exception = None
        try:
            ohandle.write('<capture>\n')
            ohandle.write('<request>\n')
            method = request.get_method()
            url = request.get_full_url() 
            parsed = urlparse.urlsplit(url)
            relative_url = parsed.path
            if parsed.query:
                relative_url += '?' + parsed.query
            if parsed.fragment:
                # TODO: will this ever happen?
                relative_url += '#' + parsed.fragment

            host = None
            request_body = None

            if hasattr(request, 'get_host'):
                host = request.get_host()
                # support 3.3
                if request.has_data():
                    request_body = request.get_data()
            else:
                host = request.host
                request_body = request.data
            
            ohandle.write('<method>%s</method>\n' % escape(method))
            ohandle.write('<url>%s</url>\n' % escape(url))
            ohandle.write('<host>%s</host>\n' % escape(host))
            try:
                # ghetto
                addr = response.fp.raw._sock.getpeername()
                if addr:
                    ohandle.write('<hostip>%s</hostip>\n' % escape(addr[0]))
            except Exception as error:
                pass
            ohandle.write('<datetime>%s</datetime>\n' % escape(time.asctime(time.gmtime())+' GMT')) # TODO: can we calculate request time and elapsed?
            request_headers = '%s %s HTTP/1.1\r\n' % (method, relative_url) # TODO: is there access to the HTTP version?
            for item in request.header_items():
                request_headers += item[0] + ': ' + '\r\n\t'.join(item[1:]) + '\r\n'

            if self.re_nonprintable_str.search(request_headers):
                ohandle.write('<headers encoding="base64">%s</headers>\n' % base64.b64encode(request_headers.encode('utf-8')).decode('ascii'))
            else:
                ohandle.write('<headers>%s</headers>\n' % escape(request_headers))
            if request_body is not None:
                if self.re_nonprintable.search(request_body):
                    ohandle.write('<body encoding="base64">%s</body>\n' % base64.b64encode(request_body).decode('ascii'))
                else:
                    ohandle.write('<body>%s</body>\n' % escape(request_body.decode('ascii')))
            ohandle.write('</request>\n')
            ohandle.write('<response>\n')
            status = int(response.getcode())
            ohandle.write('<status>%d</status>\n' % status)
            headers = response.info()
            if 'HEAD' == method or status < 200 or status in (204, 304,):
                response_body = b''
            else:
                try:
                    response_body = response.read()
                except urllib2.IncompleteRead as e:
                    saved_exception = e
            response_headers = 'HTTP/1.1 %d %s\r\n' % (status, response.msg) # TODO: is there access to the HTTP version?
            response_headers += headers.as_string()
            content_type = headers.get('Content-Type')
            content_length = headers.get('Content-Length')

            if content_type:
                ohandle.write('<content_type>%s</content_type>\n' % escape(content_type))
            if content_length:
                ohandle.write('<content_length>%d</content_length>\n' % int(content_length))

            if self.re_nonprintable_str.search(response_headers):
                ohandle.write('<headers encoding="base64">%s</headers>\n' % base64.b64encode(response_headers.encode('utf-8')).decode('ascii'))
            else:
                ohandle.write('<headers>%s</headers>\n' % escape(response_headers))
            if response_body:
                if self.re_nonprintable.search(response_body):
                    ohandle.write('<body encoding="base64">%s</body>\n' % base64.b64encode(response_body).decode('ascii'))
                else:
                    ohandle.write('<body>%s</body>\n' % escape(response_body.decode('ascii')))

            ohandle.write('</response>\n')
            ohandle.write('</capture>\n')

            self.ofhandle.write(ohandle.getvalue().encode('utf-8'))
            ohandle.close()
            
            self.write_count += 1
            if 0 == (self.write_count % self.cut_count):
                self.close()
                self.open_file()

        except Exception as e:
            sys.stderr.write('*** unhandled error in RaftCaptureProcessor: %s\n' % (e))

        if saved_exception:
            raise(saved_exception)

        return response_body
Пример #18
0
 def mocked_page_not_found(self, request):
     raise HTTPError(request.get_full_url(), 404, 'Not Found',
                     request.header_items(), None)
Пример #19
0
    def test_app_menu_callbacks(self, monkeypatch, app):
        app.mock_files({
            app.config.file_name:
            """
                [allbar]
                update_url=https://under.testing/update
            """
        })

        new_data = self.get_minimal_update_json()
        tests.mocks.urllib.mock_responses[
            'https://under.testing/update'] = tests.mocks.urllib.HTTPResponse(
                200, new_data)
        monkeypatch.setattr(urllib.request, 'urlopen',
                            tests.mocks.urllib.urlopen)

        app.refresh(None)

        mock_new_tab_url = None

        def open_new_tab(url):
            nonlocal mock_new_tab_url
            mock_new_tab_url = url

        monkeypatch.setattr(webbrowser, 'open_new_tab', open_new_tab)
        app.open_url(app.menu.mock_get_item(0))
        assert 'https://under.testing/menu-item-1' == mock_new_tab_url

        tests.mocks.urllib.mocked_requests = []
        rumps.Window.mock_response(rumps.Response(1, 'prompt_input_for_json'))
        app.send_request(app.menu.mock_get_item(1))
        assert 1 == len(tests.mocks.urllib.mocked_requests)
        request = tests.mocks.urllib.mocked_requests[0]
        assert 'https://under.testing/menu-item-2?prompt=prompt_input_for_json' == request.get_full_url(
        )
        assert 'POST' == request.get_method()
        assert [("Content-type", "application/json"),
                ("X-prompt", "prompt_input_for_json")
                ] == request.header_items()
        assert request.data == b'{"no_iter": 3.1415, "obj": {"prop_0": "abc", "prop_1": "prompt_input_for_json", "prop_2": {"prop_2_0": "xyz", "prop_2_1": [123, "prompt_input_for_json"], "prop_2_2": "prompt_input_for_json"}}, "arr": ["arr_elem_0", "prompt_input_for_json", "arr_elem_2"]}'

        tests.mocks.urllib.mocked_requests = []
        rumps.Window.mock_response(rumps.Response(0, 'prompt_input_cancelled'))
        app.send_request(app.menu.mock_get_item(1))
        assert 0 == len(tests.mocks.urllib.mocked_requests)

        tests.mocks.urllib.mocked_requests = []
        rumps.Window.mock_response(
            rumps.Response(1, 'prompt_input_for_form_encoded'))
        app.send_request(app.menu.mock_get_item(2))
        assert 1 == len(tests.mocks.urllib.mocked_requests)
        request = tests.mocks.urllib.mocked_requests[0]
        assert 'https://under.testing/menu-item-3?prompt=prompt_input_for_form_encoded' == request.get_full_url(
        )
        assert 'POST' == request.get_method()
        assert [("Content-type", "application/x-www-form-urlencoded"),
                ("X-prompt", "prompt_input_for_form_encoded")
                ] == request.header_items()
        assert request.data == b'prop_0=abc&prop_1=prompt_input_for_form_encoded'

        tests.mocks.urllib.mocked_requests = []
        rumps.Window.mock_response(
            rumps.Response(1, 'prompt_input_for_form_encoded'))
        app.send_request(app.menu.mock_get_item(3))
        assert 1 == len(tests.mocks.urllib.mocked_requests)
        request = tests.mocks.urllib.mocked_requests[0]
        assert 'https://under.testing/menu-item-4' == request.get_full_url()
        assert 'POST' == request.get_method()
        assert [("Content-type", "application/json")] == request.header_items()
        assert request.data == b'{"prop_0": "constant_data"}'

        tests.mocks.urllib.mocked_requests = []
        app.send_request(app.menu.mock_get_item(4))
        assert 1 == len(tests.mocks.urllib.mocked_requests)
        request = tests.mocks.urllib.mocked_requests[0]
        assert 'https://under.testing/menu-item-5' == request.get_full_url()
        assert 'GET' == request.get_method()
        assert [] == request.header_items()
        assert None == request.data

        app.preferences(None)
Пример #20
0
    form.add_file('newfile',
                  filename + '.txt',
                  fileHandle=open(filename + '.jsp', 'rb'))
    data = bytes(form)

    # Create a request
    request = urllib.request.Request(
        f'http://{rhost}:{rport}/CFIDE/scripts/ajax/FCKeditor/editor/filemanager/connectors/cfm/upload.cfm?Command=FileUpload&Type=File&CurrentFolder=/{filename}.jsp%00',
        data=data)
    request.add_header('Content-type', form.get_content_type())
    request.add_header('Content-length', len(data))

    # Print the request
    print('\nPriting request...')

    for name, value in request.header_items():
        print(f'{name}: {value}')

    print('\n' + request.data.decode('utf-8'))

    # Send the request and print the response
    print('\nSending request and printing response...')
    print(urllib.request.urlopen(request).read().decode('utf-8'))

    # Print some information
    print('\nPrinting some information for debugging...')
    print(f'lhost: {lhost}')
    print(f'lport: {lport}')
    print(f'rhost: {rhost}')
    print(f'rport: {rport}')
    print(f'payload: {filename}.jsp')
Пример #21
0
 def log_request(self, request):
     str = "REQUEST: %s %s" % ( request.get_method(), request.get_full_url() )
     LOGGER.debug(str)
     headers = request.header_items()
     for h in headers:
         LOGGER.debug(">> %s" % h)