Python urllib.Request() Examples

The following are 12 code examples of urllib.Request(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib , or try the search function .
Example #1
Source File: __init__.py    From pyhanlp with Apache License 2.0 6 votes vote down vote up
def hanlp_releases(cache=True):
    global HANLP_RELEASES
    if cache and HANLP_RELEASES:
        return HANLP_RELEASES
    # print('Request GitHub API')
    req = urllib.Request('http://nlp.hankcs.com/download.php?file=version')
    req.add_header('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36')
    if PY == 3:
        content = urllib.urlopen(req).read()
    else:
        content = urllib.urlopen(req).read()
    content = json.loads(content.decode())
    jar_version, jar_url, data_version, data_url = content
    meta = [(jar_version, jar_url, data_version, data_url)]
    HANLP_RELEASES = meta
    return meta 
Example #2
Source File: jf-web.py    From jawfish with MIT License 6 votes vote down vote up
def run_simulation(self):
        global TARGET, ADDR, VULN_VAR, TIMEOUT, REQ_TOTAL,\
            METHOD, OTHER_VARIABLES
        tmp = OTHER_VARIABLES
        tmp[VULN_VAR] = self.genome
        try:
            if METHOD == 0:
                prep = urllib.urlencode(tmp)
                r = urllib.urlopen('http://%s/%s' % (TARGET, ADDR), data=prep, timeout=TIMEOUT)
            else:
                prep = urllib.urlencode(tmp)
                req = urllib.Request('http://%s/%s' % (TARGET, ADDR), data=prep)
                r = urllib.urlopen(req)
            REQ_TOTAL += 1
            self.m_text['text'] = r.get_data()
            self.m_text['url'] = r.get_full_url()
            self.m_text['status_code'] = r.getcode()
        except:
            pass
        return self.m_text 
Example #3
Source File: weixin - 3.py    From ToolsCollection with GNU General Public License v2.0 6 votes vote down vote up
def deleteMember(ChatRoomName, UserNames):
    url = base_uri + '/webwxupdatechatroom?fun=delmember&pass_ticket=%s' % (pass_ticket)
    params = {
        'BaseRequest': BaseRequest,
        'ChatRoomName': ChatRoomName,
        'DelMemberList': ','.join(UserNames),
    }

    request = urllib.Request(url = url, data = json.dumps(params))
    request.add_header('ContentType', 'application/json; charset=UTF-8')
    response = urllib.urlopen(request)
    data = response.read()

    # print data

    dic = json.loads(data)
    ErrMsg = dic['BaseResponse']['ErrMsg']
    if len(ErrMsg) > 0:
        print(ErrMsg)

    Ret = dic['BaseResponse']['Ret']
    if Ret != 0:
        return False
        
    return True 
Example #4
Source File: weixin - 3.py    From ToolsCollection with GNU General Public License v2.0 5 votes vote down vote up
def getUUID():
    global uuid

    url = 'https://login.weixin.qq.com/jslogin'
    params = {
        'appid': 'wx782c26e4c19acffb',
        'fun': 'new',
        'lang': 'zh_CN',
        '_': int(time.time()),
    }

    request = urllib.Request(url = url, data = urllib.urlencode(params))
    response = urllib.urlopen(request)
    data = response.read()

    # print data

    # window.QRLogin.code = 200; window.QRLogin.uuid = "oZwt_bFfRg==";
    regx = r'window.QRLogin.code = (\d+); window.QRLogin.uuid = "(\S+?)"'
    pm = re.search(regx, data)

    code = pm.group(1)
    uuid = pm.group(2)

    if code == '200':
        return True

    return False 
Example #5
Source File: weixin - 3.py    From ToolsCollection with GNU General Public License v2.0 5 votes vote down vote up
def showQRImage():
    global tip

    url = 'https://login.weixin.qq.com/qrcode/' + uuid
    params = {
        't': 'webwx',
        '_': int(time.time()),
    }

    request = urllib.Request(url = url, data = urllib.urlencode(params))
    response = urllib.urlopen(request)

    tip = 1

    f = open(QRImagePath, 'wb')
    f.write(response.read())
    f.close()

    if sys.platform.find('darwin') >= 0:
        os.system('open %s' % QRImagePath)
    elif sys.platform.find('linux') >= 0:
        os.system('xdg-open %s' % QRImagePath)
    else:
        os.system('call %s' % QRImagePath)

    print('请使用微信扫描二维码以登录') 
Example #6
Source File: weixin - 3.py    From ToolsCollection with GNU General Public License v2.0 5 votes vote down vote up
def waitForLogin():
    global tip, base_uri, redirect_uri

    url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s' % (tip, uuid, int(time.time()))

    request = urllib.Request(url = url)
    response = urllib.urlopen(request)
    data = response.read()
    
    # print data

    # window.code=500;
    regx = r'window.code=(\d+);'
    pm = re.search(regx, data)

    code = pm.group(1)

    if code == '201': #已扫描
        print('成功扫描,请在手机上点击确认以登录')
        tip = 0
    elif code == '200': #已登录
        print('正在登录...')
        regx = r'window.redirect_uri="(\S+?)";'
        pm = re.search(regx, data)
        redirect_uri = pm.group(1) + '&fun=new'
        base_uri = redirect_uri[:redirect_uri.rfind('/')]
    elif code == '408': #超时
        pass
    # elif code == '400' or code == '500':

    return code 
Example #7
Source File: weixin - 3.py    From ToolsCollection with GNU General Public License v2.0 5 votes vote down vote up
def webwxinit():

    url = base_uri + '/webwxinit?pass_ticket=%s&skey=%s&r=%s' % (pass_ticket, skey, int(time.time()))
    params = {
        'BaseRequest': BaseRequest
    }

    request = urllib.Request(url = url, data = json.dumps(params))
    request.add_header('ContentType', 'application/json; charset=UTF-8')
    response = urllib.urlopen(request)
    data = response.read()

    if DEBUG == True:
        f = open(os.getcwd() + '/webwxinit.json', 'wb')
        f.write(data)
        f.close()

    # print data

    global ContactList, My
    dic = json.loads(data)
    ContactList = dic['ContactList']
    My = dic['User']

    ErrMsg = dic['BaseResponse']['ErrMsg']
    if len(ErrMsg) > 0:
        print(ErrMsg)

    Ret = dic['BaseResponse']['Ret']
    if Ret != 0:
        return False
        
    return True 
Example #8
Source File: weixin - 3.py    From ToolsCollection with GNU General Public License v2.0 5 votes vote down vote up
def createChatroom(UserNames):
    MemberList = []
    for UserName in UserNames:
        MemberList.append({'UserName': UserName})


    url = base_uri + '/webwxcreatechatroom?pass_ticket=%s&r=%s' % (pass_ticket, int(time.time()))
    params = {
        'BaseRequest': BaseRequest,
        'MemberCount': len(MemberList),
        'MemberList': MemberList,
        'Topic': '',
    }

    request = urllib.Request(url = url, data = json.dumps(params))
    request.add_header('ContentType', 'application/json; charset=UTF-8')
    response = urllib.urlopen(request)
    data = response.read()

    # print data

    dic = json.loads(data)
    ChatRoomName = dic['ChatRoomName']
    MemberList = dic['MemberList']
    DeletedList = []
    for Member in MemberList:
        if Member['MemberStatus'] == 4: #被对方删除了
            DeletedList.append(Member['UserName'])

    ErrMsg = dic['BaseResponse']['ErrMsg']
    if len(ErrMsg) > 0:
        print(ErrMsg)

    return (ChatRoomName, DeletedList) 
Example #9
Source File: weixin - 3.py    From ToolsCollection with GNU General Public License v2.0 5 votes vote down vote up
def addMember(ChatRoomName, UserNames):
    url = base_uri + '/webwxupdatechatroom?fun=addmember&pass_ticket=%s' % (pass_ticket)
    params = {
        'BaseRequest': BaseRequest,
        'ChatRoomName': ChatRoomName,
        'AddMemberList': ','.join(UserNames),
    }

    request = urllib.Request(url = url, data = json.dumps(params))
    request.add_header('ContentType', 'application/json; charset=UTF-8')
    response = urllib.urlopen(request)
    data = response.read()

    # print data

    dic = json.loads(data)
    MemberList = dic['MemberList']
    DeletedList = []
    for Member in MemberList:
        if Member['MemberStatus'] == 4: #被对方删除了
            DeletedList.append(Member['UserName'])

    ErrMsg = dic['BaseResponse']['ErrMsg']
    if len(ErrMsg) > 0:
        print(ErrMsg)

    return DeletedList 
Example #10
Source File: convertor.py    From pyp2rpm with MIT License 5 votes vote down vote up
def request(self, host, handler, request_body, verbose):
        self.verbose = verbose
        url = 'http://{0}{1}'.format(host, handler)
        request = urllib.Request(url)
        request.add_data(request_body)
        request.add_header("User-Agent", self.user_agent)
        request.add_header("Content-Type", "text/html")
        f = urllib.urlopen(request)
        return self.parse_response(f) 
Example #11
Source File: weixin - 3.py    From ToolsCollection with GNU General Public License v2.0 4 votes vote down vote up
def login():
    global skey, wxsid, wxuin, pass_ticket, BaseRequest

    request = urllib.Request(url = redirect_uri)
    response = urllib.urlopen(request)
    data = response.read()

    # print data

    '''
        <error>
            <ret>0</ret>
            <message>OK</message>
            <skey>xxx</skey>
            <wxsid>xxx</wxsid>
            <wxuin>xxx</wxuin>
            <pass_ticket>xxx</pass_ticket>
            <isgrayscale>1</isgrayscale>
        </error>
    '''

    doc = xml.dom.minidom.parseString(data)
    root = doc.documentElement

    for node in root.childNodes:
        if node.nodeName == 'skey':
            skey = node.childNodes[0].data
        elif node.nodeName == 'wxsid':
            wxsid = node.childNodes[0].data
        elif node.nodeName == 'wxuin':
            wxuin = node.childNodes[0].data
        elif node.nodeName == 'pass_ticket':
            pass_ticket = node.childNodes[0].data

    # print 'skey: %s, wxsid: %s, wxuin: %s, pass_ticket: %s' % (skey, wxsid, wxuin, pass_ticket)

    if skey == '' or wxsid == '' or wxuin == '' or pass_ticket == '':
        return False

    BaseRequest = {
        'Uin': int(wxuin),
        'Sid': wxsid,
        'Skey': skey,
        'DeviceID': deviceId,
    }

    return True 
Example #12
Source File: bot.py    From dungeon_bot with GNU General Public License v2.0 4 votes vote down vote up
def _requestUrl(self,
                    url,
                    method,
                    data=None):
        """Request an URL.

        Args:
          url:
            The web location we want to retrieve.
          method:
            Either POST or GET.
          data:
            A dict of (str, unicode) key/value pairs.

        Returns:
          A JSON object.
        """

        if method == 'POST':
            if 'audio' in data and (isinstance(data['audio'], file) or 'http' in data['audio']) or \
               'document' in data and (isinstance(data['document'], file) or 'http' in data['document']) or \
               'photo' in data and (isinstance(data['photo'], file) or 'http' in data['photo']) or \
               'video' in data and (isinstance(data['video'], file) or 'http' in data['video']):
                try:
                    data = InputFile(data)
                    request = urllib.Request(
                        url,
                        data=data.to_form(),
                        headers=data.headers
                    )
                    return urllib.request.urlopen(request).read()
                except urllib.error.URLError as e:
                    raise TelegramError(str(e))
            else:
                try:
                    return urllib.request.urlopen(
                        url,
                        urllib.parse.urlencode(data).encode('utf8')
                    ).read()
                #except urllib.IOError as e:
                #    raise TelegramError(str(e))
                except urllib.error.URLError as e:
                    raise TelegramError(str(e))

        if method == 'GET':
            try:
                return urllib.request.urlopen(url).read()
            except urllib.error.URLError as e:
                raise TelegramError(str(e))
        return 0