示例#1
0
    def logSites(self, packet):
        if packet.haslayer(TCP):
            data = packet.getlayer(TCP)

            try:
                request = HTTPRequest(data.load)
                print "%s %s%s" % (request.command, request.headers['host'],
                                   request.path)
            except:
                pass
示例#2
0
 def __init__(self, language="EN"):
     self.request = HTTPRequest(URI('https://www.wnp.waseda.jp/portal',
                                    'portal.php'),
                                encoding='euc-jp')
     self.request.set_dummy_headers()
     self.request.set_parameter('JavaCHK', 1)
     self.request.set_parameter('LOGINCHECK', 1)
     self.language = language
     self.set_language()
     self.logged = False
     self.logged_cnavi = False
     self._user_info = {}
     self.session_id_encode_key = None
示例#3
0
def proxy_mangle_request(req):
    """
    Takes in an HTTP request and opens the user's editor of choice to modify
    the request in JSON form. Returns a modified request.
    """

    # Get control of the terminal
    term_mutex.acquire()

    try:
        # Write json to the temp file
        with tempfile.NamedTemporaryFile(delete=False) as tf:
            tfName = tf.name
            tf.write(req_to_json(req))

        # Edit it
        editor = os.environ['EDITOR']
        subprocess.call([editor, tfName])

        # Read the temp file again
        fobj = open(tfName)
        req_dict = json.load(fobj)
        try:
            req_dict['headers']['Cookie'] = '; '.join(req_dict['headers']['Cookie'])
        except KeyError:
            pass

        # Put the headers back into lists by themselves
        for k, v in req_dict['headers'].iteritems():
            req_dict['headers'][k] = [v]
        
        newreq = HTTPRequest(method = req_dict['method'],
                             url = req.url,
                             proto = req_dict['protocol'],
                             headers = req_dict['headers'],
                             body = req_dict['body'])

        # Delete the temp file
        fobj.close()
        os.remove(tfName)

        return newreq

    finally:
        # Whenever we finish, give up control of the terminal
        term_mutex.release()
示例#4
0
    def http_req(self,
                 host: str = "127.0.0.1",
                 port: int = 50123,
                 file: str = "/",
                 method: str = "GET",
                 params: dict = None,
                 data: str = None):

        http_req = HTTPRequest(host=host)
        request = http_req.build_request(file=file,
                                         method=method,
                                         params=params,
                                         data=data)

        current_timeout = 0.1

        while True:
            self.server_socket.settimeout(current_timeout)

            if self.debug:
                print("{}{}Sending HTTP Request...{}".format(
                    bcolors.BOLD, bcolors.OKBLUE, bcolors.ENDC))
                print(request)

            self.send(message=request, host=host, port=port)

            if current_timeout > 2.0:
                raise TimeoutException("Timeout exceeded.")
                break

            try:
                data, addr = self.server_socket.recvfrom(self.buffer_size)

                if data == b"":
                    raise RuntimeError("Connection broken")
                else:
                    print("\n{}{}Received response:{}\n{}".format(
                        bcolors.BOLD, bcolors.OKBLUE, bcolors.ENDC,
                        data.decode()))
                    return self.process_response(data.decode())

            except socket.timeout:
                print("Request timed out. Trying again...\n")
                current_timeout *= 2
                continue
示例#5
0
    def http_send(self,
                  host: str = "127.0.0.1",
                  file: str = "/",
                  method: str = "GET",
                  params: dict = None,
                  data: str = None):

        http_req = HTTPRequest(host=host)
        request = http_req.build_request(file=file,
                                         method=method,
                                         params=params,
                                         data=data)

        if self.debug:
            print("{}{}Sending HTTP Request...{}".format(
                bcolors.BOLD, bcolors.OKBLUE, bcolors.ENDC))
            print(request)

        self.send(request)
示例#6
0
文件: agent.py 项目: xyzlat/palette
 def handle_method(self, method):
     self.server.log.info(method +' ' + self.path)
     res = None
     try:
         req = HTTPRequest(self, method)
         if req.path == '/archive':
             res = self.handle_archive(req)
         elif req.path == '/auth':
             res = self.handle_auth(req)
         elif req.path == '/cli':
             res = self.handle_cli(req)
         elif req.path == '/file':
             res = self.handle_file(req)
         elif req.path == '/ping':
             res = req.response
         else:
             raise http.HTTPNotFound(req.path)
     except http.HTTPException, e:
         e.handler = self
         res = e
示例#7
0
from http import HTTPRequest, HTTPResponse
from calc import Calculator
from bcolors import bcolors

http_req = HTTPRequest(host="127.0.0.1")
http_resp = HTTPResponse()
calc = Calculator()

exp = ""
while True:
    print("{}{}Type in your expression: {}".format(bcolors.OKBLUE,
                                                   bcolors.BOLD, bcolors.ENDC),
          end="")
    exp = input()

    print()

    if exp == "exit":
        break

    request = http_req.build_request(method="POST", params={"expression": exp})

    print("{}Making HTTP request...{}".format(bcolors.OKGREEN, bcolors.ENDC))
    print("----------------------")
    print(request, "\n")

    print("{}Evaluating expression...{}".format(bcolors.OKGREEN, bcolors.ENDC))
    print("------------------------")

    try:
        result = calc.evaluate(exp)
import socket
from http import HTTPRequest, HTTPResponse, HTTPParser

server = "www.inspirasonho.com.br"
file = "/sobre"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((server, 80))

hreq = HTTPRequest(host=server)
request = hreq.build_request(method="GET", file=file)

print("Request:")
print(request)
print("----\n")

s.send(request.encode())

response = s.recv(4096)

print("Response:")
print(response.decode())
print()
print("Response Length:", len(response))