예제 #1
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
예제 #2
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)
예제 #3
0
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)

        print("Calculation result:", result, "\n")

        print("{}Retrieving HTTP response...{}".format(bcolors.OKGREEN,
                                                       bcolors.ENDC))
예제 #4
0
from http import HTTPRequest, HTTPResponse, HTTPParser

http_parser = HTTPParser()

http_req = HTTPRequest(host="127.0.0.1")

print(http_req.build_request(method="POST", params={"expression": "+ 1 2"}),
      "\n")

request1 = http_req.build_request(method="POST",
                                  file="/testfilename",
                                  params={"expression": "* 5 6"})

print("Request 1:")
print(request1)
print("Parsed request:", http_parser.parse_request(request1))

http_resp = HTTPResponse()

response1 = http_resp.build_response(status=406, data=-1)
parsed_response1 = http_parser.parse_response(response1)
print()

print("Response 1:")
print(response1, "\n")
print("Status:", parsed_response1['status'])
print("Header Fields:", parsed_response1['fields'])
print("Data:", parsed_response1['data'])

response2 = http_resp.build_response(status=200, data="10")
parsed_response2 = http_parser.parse_response(response2)
예제 #5
0
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))