<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def porthistory(port_number, start_date=None, end_date=None, return_format=None):
 """Returns port data for a range of dates. In the return data: Records: Total number of records for a given date range. Targets: Number of unique destination IP addresses. Sources: Number of unique originating IPs. :param port_number: a valid port number (required) :param start_date: string or datetime.date(), default is 30 days ago :param end_date: string or datetime.date(), default is today """

    uri = 'porthistory/{port}'.format(port=port_number)

    if not start_date:
        # default 30 days ago
        start_date = datetime.datetime.now() - datetime.timedelta(days=30)

    try:
        uri = '/'.join([uri, start_date.strftime("%Y-%m-%d")])
    except AttributeError:
        uri = '/'.join([uri, start_date])
    if end_date:
        try:
            uri = '/'.join([uri, end_date.strftime("%Y-%m-%d")])
        except AttributeError:
            uri = '/'.join([uri, end_date])
    response = _get(uri, return_format)
    if 'bad port number' in str(response):
        raise Error('Bad port, {port}'.format(port=port_number))
    else:
        return response