<commit_before># Echo client program
import socket
import sys
import xml
import time
import re
import collections
import json
import comp_urllib

SSDP_ADDR = "172.16.58.3"  # The remote host
SSDP_PORT = 1900    # The same port as used by the server
SSDP_MX = 1
SSDP_ST = "urn:schemas-sony-com:service:ScalarWebAPI:1"
SSDP_TIMEOUT = 10000  #msec
PACKET_BUFFER_SIZE = 1024

# Find all available cameras using uPNP
# Improved with code from 'https://github.com/storborg/sonypy' under MIT license.

class ControlPoint(object):
    def __init__(self):
        self.__bind_sockets()

    def __bind_sockets(self):
        self.__udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.__udp_socket.settimeout(0.1)
        return

    def discover(self, duration=None):
        # Default timeout of 1s
        if duration==None:
            duration=1

        # Set the socket to broadcast mode.
        self.__udp_socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL , 2)

        msg = '\r\n'.join(["M-SEARCH * HTTP/1.1",
                           "HOST: 172.16.58.3:1900",
                           "MAN: ssdp:discover",
                           "MX: " + str(duration),
                           "ST: " + SSDP_ST,
                           "USER-AGENT: ",
                           "",
                           ""])

        # Send the message.
        msg_bytes = bytearray(msg, 'utf8')
        self.__udp_socket.sendto(msg_bytes, (SSDP_ADDR, SSDP_PORT))

        # Get the responses.
        packets = self._listen_for_discover(duration)
        endpoints = []
        for host,addr,data in packets:
            resp = self._parse_ssdp_response(data)
            try:
                endpoint = self._read_device_definition(resp['location'])
                endpoints.append(endpoint)
            except:
                pass
        return endpoints

    def _listen_for_discover(self, duration):
        start = time.time()
        packets = []
        while (time.time() < (start + duration)):
            try:
                data, (host, port) = self.__udp_socket.recvfrom(1024)

                # Assemble any packets from multiple cameras
                found = False
                for x in range(len(packets)):
                    ohost, oport, odata = packets[x]
                    if host == ohost and port == oport:
                        packets.append((host, port, odata+data))
                        packets.pop(x)
                        found = True

                if not found:
                    packets.append((host, port, data))
            except:
                pass
        return packets

    def _parse_ssdp_response(self, data):
        lines = data.split('\r\n')
        assert lines[0] == 'HTTP/1.1 200 OK'
        headers = {}
        for line in lines[1:]:
            if line:
                try:
                    key, val = line.split(': ', 1)
                    headers[key.lower()] = val
                except:
                    pass
        return headers

    def _parse_device_definition(self, doc):
        """
        Parse the XML device definition file.
        """
        dd_regex = ('<av:X_ScalarWebAPI_Service>'
            '\s*'
            '<av:X_ScalarWebAPI_ServiceType>'
            '(.+?)'
            '</av:X_ScalarWebAPI_ServiceType>'
            '\s*'
            '<av:X_ScalarWebAPI_ActionList_URL>'
            '(.+?)'
            '/sony'                               # and also strip '/sony'
            '</av:X_ScalarWebAPI_ActionList_URL>'
            '\s*'
            '<av:X_ScalarWebAPI_AccessType\s*/>'  # Note: QX10 has 'Type />', HX60 has 'Type/>'
            '\s*'
            '</av:X_ScalarWebAPI_Service>')

        services = {}
        for m in re.findall(dd_regex, doc):
            service_name = m[0]
            endpoint = m[1]
            services[service_name] = endpoint
        return services

    def _read_device_definition(self, url):
        """
        Fetch and parse the device definition, and extract the URL endpoint for
        the camera API service.
        """
        r = comp_urllib.urlopen(url)
        services = self._parse_device_definition(r.read())

        return services['camera']


# Common Header
# 0--------1--------2--------+--------4----+----+----+----8
# |0xFF    |payload | sequence number | Time stamp        |
# |        |type    |                 |                   |
# +-------------------------------------------------------+
#<commit_msg>Fix type errors in discover() of ControlPoint.<commit_after># Echo client program
import socket
import sys
import xml
import time
import re
import collections
import json
import comp_urllib

SSDP_ADDR = "172.16.58.3"  # The remote host
SSDP_PORT = 1900    # The same port as used by the server
SSDP_MX = 1
SSDP_ST = "urn:schemas-sony-com:service:ScalarWebAPI:1"
SSDP_TIMEOUT = 10000  #msec
PACKET_BUFFER_SIZE = 1024

# Find all available cameras using uPNP
# Improved with code from 'https://github.com/storborg/sonypy' under MIT license.

class ControlPoint(object):
    def __init__(self):
        self.__bind_sockets()

    def __bind_sockets(self):
        self.__udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.__udp_socket.settimeout(0.1)
        return

    def discover(self, duration=None):
        # Default timeout of 1s
        if duration==None:
            duration=1

        # Set the socket to broadcast mode.
        self.__udp_socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL , 2)

        msg = '\r\n'.join(["M-SEARCH * HTTP/1.1",
                           "HOST: 172.16.58.3:1900",
                           "MAN: ssdp:discover",
                           "MX: " + str(duration),
                           "ST: " + SSDP_ST,
                           "USER-AGENT: ",
                           "",
                           ""])

        # Send the message.
        msg_bytes = bytearray(msg, 'utf8')
        self.__udp_socket.sendto(msg_bytes, (SSDP_ADDR, SSDP_PORT))

        # Get the responses.
        packets = self._listen_for_discover(duration)
        endpoints = []
        for host,addr,data in packets:
            resp = self._parse_ssdp_response(data)
            try:
                endpoint = self._read_device_definition(resp['location'])
                endpoints.append(endpoint)
            except:
                pass
        return endpoints

    def _listen_for_discover(self, duration):
        start = time.time()
        packets = []
        while (time.time() < (start + duration)):
            try:
                data, (host, port) = self.__udp_socket.recvfrom(1024)

                # Assemble any packets from multiple cameras
                found = False
                for x in range(len(packets)):
                    ohost, oport, odata = packets[x]
                    if host == ohost and port == oport:
                        packets.append((host, port, odata+data))
                        packets.pop(x)
                        found = True

                if not found:
                    packets.append((host, port, data))
            except:
                pass
        return packets

    def _parse_ssdp_response(self, data):
        data_str = data.decode('utf8')
        lines = data_str.split('\r\n')
        assert lines[0] == 'HTTP/1.1 200 OK'
        headers = {}
        for line in lines[1:]:
            if line:
                try:
                    key, val = line.split(': ', 1)
                    headers[key.lower()] = val
                except:
                    pass
        return headers

    def _parse_device_definition(self, doc):
        """
        Parse the XML device definition file.
        """
        dd_regex = ('<av:X_ScalarWebAPI_Service>'
            '\s*'
            '<av:X_ScalarWebAPI_ServiceType>'
            '(.+?)'
            '</av:X_ScalarWebAPI_ServiceType>'
            '\s*'
            '<av:X_ScalarWebAPI_ActionList_URL>'
            '(.+?)'
            '/sony'                               # and also strip '/sony'
            '</av:X_ScalarWebAPI_ActionList_URL>'
            '\s*'
            '<av:X_ScalarWebAPI_AccessType\s*/>'  # Note: QX10 has 'Type />', HX60 has 'Type/>'
            '\s*'
            '</av:X_ScalarWebAPI_Service>')

        doc_str = doc.decode('utf8')
        services = {}
        for m in re.findall(dd_regex, doc_str):
            service_name = m[0]
            endpoint = m[1]
            services[service_name] = endpoint
        return services

    def _read_device_definition(self, url):
        """
        Fetch and parse the device definition, and extract the URL endpoint for
        the camera API service.
        """
        r = comp_urllib.urlopen(url)
        services = self._parse_device_definition(r.read())

        return services['camera']


# Common Header
# 0--------1--------2--------+--------4----+----+----+----8
# |0xFF    |payload | sequence number | Time stamp        |
# |        |type    |                 |                   |
# +-------------------------------------------------------+
#
{'fraction_non_alphanumeric': 0.12534209085933223, 'fraction_numerical': 0.014887794198139025, 'mean_word_length': 1.457235072619688, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1247824', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2560, 'n_words': 596}