Ejemplo n.º 1
0
    def listenToClient(self, client, address):
        size = 1024
        #print(type(client))
        print("Connected ", address)
        while True:
            try:
                data = client.recv(size)

                if data:
                    # Set the response to echo back the recieved data
                    response = 'ok'
                    client.send(response.encode())

                    obj = pickle.loads(data)
                    #print(obj)
                    test = obj.split(',')
                    print(test)
                    #################################################
                    xml_request = str(xml_head + make_body(test[0], test[1]) +
                                      xml_tail)
                    conn.request("POST",
                                 "/epcis/Service/EventCapture",
                                 headers=headers,
                                 body=xml_request.encode('utf-8'))
                    res = conn.getresponse()
                    data = res.read()
                    print(data.decode("utf-8"))
                    #################################################

                else:
                    raise error('Client disconnected')
            except:
                client.close()
                return False
Ejemplo n.º 2
0
def sendMsgtoUser(msg):
    thread_id = '100007842240328'
    thread_type = ThreadType.USER
    client = Client("0966880147", "mekiep")
    client.send(Message(text=msg),
                thread_id=thread_id,
                thread_type=thread_type)
Ejemplo n.º 3
0
def extract_html_page(target_host):
    global my_var
    target_host2 = urlparse(target_host).netloc
    if not os.path.exists(dstPath):
        os.makedirs(dstPath)
    if not (os.path.isdir(os.path.join(dstPath, target_host2))):
        os.mkdir(os.path.join(dstPath, target_host2))
    client = "RIWEB_CRAWLER"
    target_port = 80
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((target_host2, target_port))

    request = "GET {} HTTP/1.1\r\nHost: {}\r\nUser-Agent: {}\r\n\r\n".format(
        target_host, target_host2, client)

    client.send(request.encode())
    #din lab6
    CHUNK_SIZE = 16
    buffer = bytearray()
    buffer.extend(client.recv(CHUNK_SIZE))
    firstline = buffer.decode()
    data = b''
    # verificarea codului de stare (daca acesta este cod de eroare, se va deschide un fisier text în care se va
    # scrie cererea initiala si raspunsul  serverului);
    if '200 OK' in firstline:
        buffer_size = 4096
        data1 = b''
        while True:
            part = client.recv(buffer_size)
            data1 += part
            if b'</html>' in part.lower() or len(part) == 0:
                break
        data = data1
        data = data.decode()
        data_lower = data.lower()
        if data_lower.find('<!doctype') > -1:
            data_to_store = data[data_lower.find('<!doctype'
                                                 ):data_lower.find('</html>') +
                                 8]
        else:
            data_to_store = data[data_lower.
                                 find('<html'):data_lower.find('</html>') + 8]
            # salvarea continutului raspunsului (corpul mesajului – pagina html) într-un fisier html
        with open('my_page.html', 'w') as file:
            file.write(data_to_store)
    else:
        data = data + buffer
        buffer_size = 4096
        data1 = b''
        while True:
            part = client.recv(buffer_size)
            data1 += part
            if b'</html>' in part.lower() or len(part) == 0:
                break
        data += data1
        with open('error_page.txt', 'w') as file:
            file.write(data.decode())
        pass
Ejemplo n.º 4
0
def extract_html_page(target_host):
    global my_var
    #target_host2 = "riweb.tibeica.com"
    target_host2 = urlparse(target_host).netloc
    if not (os.path.isdir(os.path.join('work_directory', target_host2))):
        os.mkdir(os.path.join('work_directory', target_host2))
    client = "RIWEB_CRAWLER"
    target_port = 80  # create a socket object
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # connect the client
    client.connect((target_host2, target_port))

    # send some data
    request = "GET {} HTTP/1.1\r\nHost: {}\r\nUser-Agent: {}\r\n\r\n".format(
        target_host, target_host2, client)

    client.send(request.encode())

    # =========================Read step by step===============================
    CHUNK_SIZE = 36  # you can set it larger or smaller
    lines = []
    buffer = bytearray()
    buffer.extend(client.recv(CHUNK_SIZE))
    firstline = buffer[:buffer.find(b'\n')]
    firstline = buffer.decode()
    data = b''
    #print(target_host)
    #print(buffer)
    if '200 OK' in firstline:

        data = recvall(client)
        data = data.decode()
        data_lower = data.lower()
        # print(data_lower.find('<!doctype'))
        if data_lower.find('<!doctype') > -1:
            data_to_store = data[data_lower.find('<!doctype'
                                                 ):data_lower.find('</html>') +
                                 8]
        else:
            data_to_store = data[data_lower.
                                 find('<html'):data_lower.find('</html>') + 8]
        #client.close()
        return data_to_store
    else:
        print(firstline)
        print(target_host)
        data = data + buffer
        data += recvall(client)
        with open('error_page.txt', 'w') as file:
            file.write(data.decode())
        pass
Ejemplo n.º 5
0
def forward_response(resp, client):
    fields = []
    fields.append('HTTP/1.1 ')
    fields.append(str(resp.status) + ' ' + resp.msg + ' \n')
    fields.append('Content-Length: ' + resp.getheader('Content-Length') + '\n')
    fields.append('Content-Type: ' + resp.getheader('Content-Type') + '\n\n')
    data = resp.read()
    fields.append(data)

    response = b''
    for data in fields:
        try:
            response += data
        except:
            response += bytes(str(data), 'utf-8')
    client.send(response)
Ejemplo n.º 6
0
def post_multipart(host, selector, fields, files):
    """
    Post fields and files to an http host as multipart/form-data.
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be
    uploaded as files
    Return the server's response page.
    """
    content_type, body = encode_multipart_formdata(fields, files)
    client = http.client.HTTP(host)
    client.putrequest('POST', selector)
    client.putheader('content-type', content_type)
    client.putheader('content-length', str(len(body)))
    client.endheaders()
    client.send(body)
    errcode, errmsg, headers = client.getreply()
    return client.file.read()
Ejemplo n.º 7
0
def mainland(phone, code):
    api_url = 'https://sms_developer.zhenzikj.com'
    app_id = '100207'  #榛子云id
    app_secret = '062ba82e-5c9b-4702-9105-feb2173123cb'  #榛子云秘钥
    client = smsclient.ZhenziSmsClient(apiUrl=api_url,
                                       appId=app_id,
                                       appSecret=app_secret)
    code = code
    result = client.send(phone, '云区块---您的验证码为:%r' % code)
    return result
Ejemplo n.º 8
0
    def test_checking_lack_of_origin_succeeds(self):
        server = self.loop.run_until_complete(
            serve(handler, 'localhost', 8642, origins=['']))
        client = self.loop.run_until_complete(connect('ws://localhost:8642/'))

        self.loop.run_until_complete(client.send("Hello!"))
        self.assertEqual(self.loop.run_until_complete(client.recv()), "Hello!")

        self.loop.run_until_complete(client.close())
        server.close()
        self.loop.run_until_complete(server.wait_closed())
Ejemplo n.º 9
0
    def _unix_connector(self, message):
        """ : param message: bytes """
        CRLF = "\r\n"
        init_line = ["POST /checkv2 HTTP/1.1"]
        self.add_header("Content-Length", len(message))
        headers = init_line + [f"{header[0]}: {header[1]}" for header in self._headers]
        headers = (CRLF.join(headers) + 2*CRLF).encode("utf8")

        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        client.connect(self._connection_string)
        raddr = client.getpeername()
        print(f"{self.msg_id}: localhost -> {raddr}: Connected to maild.", on_debug=True)
        print(f"{self.msg_id}: localhost -> {raddr}: Send message to maild.", on_debug=True)
        client.send(headers + message)
        print(f"{self.msg_id}: localhost <- {raddr}: Waiting for response from maild.", on_debug=True)
        rspamd_result = client.recv(1024)
        if not rspamd_result:
            return {"error": "Error: Rspamd server is not responding"}
        headers, body = rspamd_result.decode("utf8").split("\r\n\r\n")
        client.close()
        return json.loads(body)
Ejemplo n.º 10
0
def serverit():
    """
    ('127.0.0.1', 50051) connected
    :return:
    """

    # SOCK_STREAM=tcp SOCK_DGRAM=udp
    server = socket(family=AF_INET, type=SOCK_STREAM)

    server.bind(("127.0.0.1", 4321))

    # queue size
    server.listen(512)

    while True:
        client, addr = server.accept()

        print('{} connected'.format(addr))

        client.send('{}'.format(datetime.now()).encode('utf-8'))

        client.close()