示例#1
0
    def send_file(self, filename: str, device: str, port: int = None, data=None, service_name='OBEX Object Push',
                  binary: bool = False):
        """
        Send a local file to a device that exposes an OBEX Object Push service

        :param filename: Path of the file to be sent
        :param data: Alternatively to a file on disk you can send raw (string or binary) content
        :param device: Device address or name
        :param port: Port number
        :param service_name: Service name
        :param binary: Set to true if data is a base64-encoded binary string
        """
        from PyOBEX.client import Client

        if not data:
            filename = os.path.abspath(os.path.expanduser(filename))
            with open(filename, 'r') as f:
                data = f.read()
            filename = os.path.basename(filename)
        else:
            if binary:
                data = base64.decodebytes(data.encode() if isinstance(data, str) else data)

        addr, port, protocol = self._get_addr_port_protocol(device=device, port=port,
                                                            service_name=service_name)

        client = Client(addr, port)
        self.logger.info('Connecting to device {}'.format(addr))
        client.connect()
        self.logger.info('Sending file {} to device {}'.format(filename, addr))
        client.put(filename, data)
        self.logger.info('File {} sent to device {}'.format(filename, addr))
        client.disconnect()
示例#2
0
def RFCOMM_Sendfile(address, fname="test.txt", fdata=b'Hello world\n'):

    print("Searching for OBEX service on %s" % address)

    # Use the server named "OBEX Object Push" to send file
    service_matches = bluetooth.find_service(name="OBEX Object Push",
                                             address=address)
    if len(service_matches) == 0:
        print("Couldn't find the service")
        sys.exit(0)

    match = service_matches[0]
    name = match["name"]
    host = match["host"]
    port = match["port"]

    print("Service Name: %s" % match["name"])
    print("    Host:        %s" % match["host"])
    print("    Description: %s" % match["description"])
    print("    Provided By: %s" % match["provider"])
    print("    Protocol:    %s" % match["protocol"])
    print("    channel/PSM: %s" % match["port"])
    print("    svc classes: %s " % match["service-classes"])
    print("    profiles:    %s " % match["profiles"])
    print("    service id:  %s " % match["service-id"])

    print("Connecting to \"%s\" on %s" % (name, host))
    client = Client(host, port)
    client.connect()

    # The parameter [file_data] must be bytes type like [b'****'],otherwise there will be some error occur in Python3
    client.put(name=fname, file_data=fdata)
    client.disconnect()

    return
示例#3
0
def sendFile(name, host, port):
    print("Connecting to \"%s\" on %s" % (name, host))
    client = Client(host, port)
    client.connect()
    file_to_send = read_file()
    client.put("fileToSend.txt", file_to_send)
    client.disconnect()
def send_text_file(bd_addr, port, filename):
    res = bytes("my name is Safwan Mansuri", 'utf-8')

    client = Client(bd_addr, port)
    client.connect()
    print('\n')
    print("Waiting for acceptance......")
    client.put(filename, res)
    client.disconnect()
def send_music(bd_addr, port, filename):
    Image = open(filename, "rb")
    data = Image.read()

    client = Client(bd_addr, port)
    client.connect()
    print('\n')
    print("Waiting for acceptance......")
    client.put(filename, data)
    client.disconnect()
示例#6
0
class BT:
    def __init__(self, duration=8):
        self.duration = duration
        self.nearby_devices = discover_devices(duration=self.duration,
                                               lookup_names=True)
        self.client = None

    def refresh(self):
        logging.info('Please wait, upddating the list of nearby devices...')
        self.nearby_devices = discover_devices(duration=self.duration,
                                               lookup_names=True)

    def get_nearby_devices(self):
        return self.nearby_devices

    def connect(self):
        self.refresh()
        print('Sr.\tAddress \t\tName')
        for i in range(len(self.nearby_devices)):
            print(
                f'{i + 1}.\t{self.nearby_devices[i][0]}\t{self.nearby_devices[i][1]}'
            )
        addr = self.nearby_devices[int(
            input(
                '\nEnter the number of the device, to which you wish to connect : '
            )) - 1][0]
        service = find_service(name=b'OBEX Object Push\x00', address=addr)[0]
        logging.info(
            f'Connecting to {service["host"]} on port {service["port"]}...')
        self.client = Client(service["host"], service["port"])
        self.client.connect()
        logging.info('Connection success.')

    def disconnect(self):
        logging.info(f'Disconnecting from : {self.client.address}...')
        self.client.disconnect()
        self.client = None
        logging.info('Disconnected successfully.')

    def send_file(self, file_name, binary_data=None):
        self.connect()
        logging.info('Sending the file now...')
        if binary_data is None:
            with open(file_name, 'rb') as f:
                contents = f.read()
            self.client.put(file_name, contents)
        else:
            self.client.put(file_name, binary_data)
        logging.info('File has been sent successfully.')
        self.disconnect()
示例#7
0
def blue_send(device, files=[]):
    service_matches = find_service(name=b'OBEX Object Push\x00',
                                   address=device)
    first_match = service_matches[0]
    port = first_match["port"]
    name = first_match["name"]
    host = first_match["host"]
    client = Client(host, port)
    client.connect()
    for file in files:
        file_content = open(file, 'rb').readlines()
        if type(file_content) == list:
            file_content = b''.join(file_content).strip()
        client.put('Output\\' + file, file_content)
    client.disconnect()
示例#8
0
class FTPclient(threading.Thread):
    def __init__(self, address, port, file_path, file_name):
        threading.Thread.__init__(self)
        self.address = address
        self.port = port
        self.client = Client(self.address, self.port)
        self.file_path = file_path
        self.file_name = file_name

    def run(self):
        try:
            self.client.connect()
            self.client.put(self.file_name, open(self.file_path).read())
            print '********************************************************************'
            print self.address + ' : File Transfer Success!!!'
            print '********************************************************************'
        except:
            print '********************************************************************'
            print self.address + + ' : File Transfer Failed...'
            print '********************************************************************'
        self.client.disconnect()
        self.client.delete
示例#9
0
def bluetooth(target_name, file_name):

    file_contents = read_file(file_name)
    if file_contents == "":
        return 0

    target_address = None
    nearby_devices = bt.discover_devices()

    for bdaddr in nearby_devices:
        if target_name == bt.lookup_name(bdaddr):
            target_address = bdaddr
            break

    if target_address is not None:
        print("Found device with MAC Address:  " + target_address)
    else:
        print("Couldn't find device")
        return 0

    services = bt.find_service(address=target_address,
                               name=b'OBEX Object Push\x00')

    if len(services) == 0:
        print("Couldn't find service")
        return 0

    first_match = services[0]
    port = first_match["port"]
    name = first_match["name"]
    host = first_match["host"]

    client = Client(host, port)
    client.connect()
    client.put(file_name, file_contents)
    client.disconnect()
    return 1
示例#10
0
from bluetooth import *
from PyOBEX.client import Client
import sys

addr = sys.argv[1]
print("Searching for OBEX service on %s" % addr)

service_matches = find_service(name=b'OBEX Object Push\x00', address=addr)
if len(service_matches) == 0:
    print("Couldn't find the service.")
    sys.exit(0)

first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]

print("Connecting to \"%s\" on %s" % (name, host))
client = Client(host, port)
client.connect()
client.put("test.txt", "Hello world\n")
client.disconnect()
示例#11
0
import bluetooth, os, sys
from xml.etree import ElementTree
from PyOBEX import client, responses
from PyOBEX.client import Client
if __name__ == "__main__":
    device_addr = sys.argv[1]
    path = sys.argv[2]

    services = bluetooth.find_service(uuid="0000112f", address=device_addr)
    if services:
        port = services[0]["port"]
        names = services[0]["name"]

    print("Connecting to port %s with %s" % (port, names))
    c = Client(device_addr, port)
    response = c.connect()

    pieces = path.split("/")
    for piece in pieces:
        print(piece)
        response = c.setpath(piece)

    sys.stdout.write("Enteres directory %s \n" % path)
    response = c.listdir()
    if isinstance(responce, responses.FailureResponse):
        sys.stderr.write('Failed to enter directory\n')
        sys.exit(1)
    headers, data = response
    tree = ElementTree.formstring(data)
    for element in tree.findall("file"):
        name = element.attrib["name"]