def download_ftp(url, filepath, shared_args):
    """ download an FTP resource. """

    total_size = 0

    try:
        request = urllib.request.urlopen(url)
        total_size = int(request.getheader('Content-Length').strip())
    except urllib.error.URLError as e:
        print(url)
        error_string = '\n    url: {0} does not exist, trying other sources\n'.format(
            url)

        raise RemoteFileException(error_string)

    downloaded = 0
    filename = filepath[len(filepath) - filepath[::-1].index('/'):]

    with open(filepath, 'wb') as fileobj:
        while True:
            output_string = "    Downloading %s - %.1fMB of %.1fMB\r" % (
                filename, (downloaded / 1000000), (total_size / 1000000))

            print_output(shared_args, output_string)

            chunk = request.read(CHUNK)
            if not chunk:
                break
            fileobj.write(chunk)
            downloaded += len(chunk)

        output_string = "\n     Download completed..."
        print_output(shared_args, output_string)

    return filepath
Пример #2
0
def get_canteens():
    request = urllib.request.urlopen(CANTEENS_URL)
    last = get_last_page(request.getheader('link'))

    all_canteens = list()
    for i in range(1, last + 1):
        canteen_list = urllib.request.urlopen(
            f'{CANTEENS_URL}?page={i}').read()
        data = json.loads(canteen_list)
        all_canteens.extend(data)

    return all_canteens
Пример #3
0
def NaverAPI():
    server = 'openapi.naver.com'
    client_id = 'CauJEcypbFDul3iDdw3V'
    client_secret = '1gsH15h8bj'
    conn = http.client.HTTPSConnection(server)
    conn.request('GET', '/v1/serch/book.xml?query=love&display=10&start=1',
                 None, {
                     'X-Naver-Clinet-Id': client_id,
                     'X-Naver-Client-Secret': client_secret
                 })
    req = conn.getresponse()
    cLen = req.getheader("Content-Length")
    req.read(int(cLen))
Пример #4
0
    def login_detail(self, account, password, dev, device, r, m):
        return True
        try:
            #like: http://127.0.0.1:8080/msg?c=1&a=BOS30000022&p=&ver=1.2.0&dev=4730d114e32c13359a112ce6cc17eebbd2073944&device=android&r=1&m=0&sign=7e343d61e73ecd3c600521ae9588c460

            c = proto.message.Player.Login.value
            sign = proto.message.get_sign(c)
            url = "http://%s:%d/msg?c=%d&a=%s&p=%s&ver=%s&dev=%s&device=%s&r=%d&m=%d&sign=%s" % (
                self.addr, self.port, c, account, password, self.ver, dev,
                device, r, m, sign)
            print("url=", url)

            request = urllib.request.urlopen(url)
            result = request.read()

            self.cookie = request.getheader("Set-Cookie")
            if self.cookie != "":
                self.cookie = self.cookie[self.cookie.find("=") +
                                          1:self.cookie.find(";")]

            cmd, msg = proto.message.unpack(result)
            pmsg = proto.player_pb2.RetLoginMsg()
            pmsg.ParseFromString(msg)
            proto.myprint.print_login_result(pmsg)

            self.account_name = pmsg.Account
            self.id = pmsg.Id

            if pmsg.Id == 0:
                print("connect login server fail.")
                return False

            # TODO: 暂时屏蔽登录gateway
            '''
            if self.pystress == False:
                if gateway.gate.login_gateway(self) == False:
                    print("connect gateway server fail.")
                    return False
            '''

        except Exception as e:
            print(e)
            return False

        return True
Пример #5
0
    def login_detail(self, account, password, mode, userdata):
        try:
            cmd = LoginCmd.Login.value
            msg = proto.login_pb2.MsgLogin()
            msg.account = account
            msg.password = password
            msg.mode = mode
            #msg.userdata = userdata
            request, result = self.send(cmd, msg)

            self.cookie = request.getheader("Set-Cookie")
            if self.cookie != None and self.cookie != "":
                self.cookie = self.cookie[self.cookie.find("=") +
                                          1:self.cookie.find(";")]

        except Exception as e:
            print(traceback.format_exc())
            return False
        return True
Пример #6
0
#!/usr/bin/python3
''' displays X-Request-Id in header of response '''
import urllib.request
from sys import argv

if __name__ == "__main__":
    URL = argv[1]
    with urllib.request.urlopen(URL) as request:
        print(request.getheader('X-Request-Id'))