def synthesize(text): # Don't modify the original object parameters = copy.copy(syn_parameters) # Set the text parameters['text'] = text print(urlencode(parameters)) # Request foo! r = requests.get( 'https://text-to-speech-demo.mybluemix.net/api/synthesize?' + urlencode(parameters)) return r
def getImg(): data = { "login_site": "E", "module": "login", "rand": "sjrand", "0.17231872703389062": "" } # 获取验证码 param = parse.urlencode(data) url = "https://kyfw.12306.cn/passport/captcha/captcha-image?{}".format(param) response = session.get(url, headers=headers) if response.status_code == 200: file = BytesIO(response.content) img = Image.open(file) img.show() try: im = Image.open('img.jpg') # 展示验证码图片,会调用系统自带的图片浏览器打开图片,线程阻塞 im.show() # 关闭,只是代码关闭,实际上图片浏览器没有关闭,但是终端已经可以进行交互了(结束阻塞) im.close() except: print u'[*] 请输入验证码' # ======================================================================= captcha_solution = raw_input('[*] 请输入验证码位置,以","分割[例如2,5]:') return captcha_solution
def get(self): get_url = self.url + "?" + parse.urlencode(body) if not timeouts: res = requests.get(get_url, headers=headers) else: timeout = (timeouts["connecttimout"], timeouts["readtimeout"]) res = requests.get(get_url, headers=headers, timeout=timeouts) return res
def read_page(url, page_num, keyword): # 模仿浏览器post需求信息,并读取返回后的页面信息 page_headers = { 'Host': 'www.lagou.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3', 'Connection': 'keep-alive' } if page_num == 1: boo = 'true' else: boo = 'false' page_data = parse.urlencode([ # 通过页面分析,发现浏览器提交的FormData包括以下参数 ('first', boo), ('pn', page_num), ('kd', keyword) ]) req = request.Request(url, headers=page_headers) page = request.urlopen(req, data=page_data.encode('utf-8')).read() page = page.decode('utf-8') return page
def send_sms(self, mobile, sms_info): """发送手机通知短信,用的是-互亿无线-的测试短信""" host = "106.ihuyi.com" sms_send_uri = "/webservice/sms.php?method=Submit" account = "C59782899" pass_word = "19d4d9c0796532c7328e8b82e2812655" params = parse.urlencode({ 'account': account, 'password': pass_word, 'content': sms_info, 'mobile': mobile, 'format': 'json' }) headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain" } conn = httplib2.HTTPConnectionWithTimeout(host, port=80, timeout=30) conn.request("POST", sms_send_uri, params, headers) response = conn.getresponse() response_str = response.read() conn.close() return response_str
def get(url, params=None, **kw): if params: url = url.rstrip('?') + '?' + urlencode(params, doseq=True) #print("url with parameters: " + url) return urequests.get(url, **kw)
def request(method, url, params=None, cookies=None, data=None, json=None, headers={}, parse_headers=True, followRedirect=True): if params is not None: if params != {}: url = url.rstrip('?') + '?' + urlencode(params, doseq=True) redir_cnt = 1 while True: try: proto, dummy, host, path = url.split("/", 3) except ValueError: proto, dummy, host = url.split("/", 2) path = "" if proto == "http:": port = 80 elif proto == "https:": import ussl port = 443 else: raise ValueError("Unsupported protocol: " + proto) if ":" in host: host, port = host.split(":", 1) port = int(port) ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM) ai = ai[0] resp_d = None if parse_headers is not False: resp_d = {} # print('Socket create') s = usocket.socket(ai[0], ai[1], ai[2]) # 60sec timeout on blocking operations s.settimeout(60.0) try: # print('Socket connect') s.connect(ai[-1]) if proto == "https:": s = ussl.wrap_socket(s, server_hostname=host) # print('Socket wrapped') s.write(b"%s /%s HTTP/1.0\r\n" % (method, path)) # print('Socket write: ') # print(b"%s /%s HTTP/1.0\r\n" % (method, path)) if "Host" not in headers: s.write(b"Host: %s\r\n" % host) # Iterate over keys to avoid tuple alloc for k in headers: s.write(k) s.write(b": ") s.write(headers[k]) s.write(b"\r\n") # print(k, b": ".decode('utf-8'), headers[k], b"\r\n".decode('utf-8')) if cookies is not None: for cookie in cookies: s.write(b"Cookie: ") s.write(cookie) s.write(b"=") s.write(cookies[cookie]) s.write(b"\r\n") if json is not None: assert data is None import ujson data = ujson.dumps(json) s.write(b"Content-Type: application/json\r\n") if data: s.write(b"Content-Length: %d\r\n" % len(data)) # print("Content-Length: %d\r\n" % len(data)) s.write(b"Connection: close\r\n\r\n") if data: s.write(data) # print(data) # print('Start reading http') l = s.readline() #print('Received protocoll and resultcode %s' % l.decode('utf-8')) l = l.split(None, 2) status = int(l[1]) reason = "" if len(l) > 2: reason = l[2].rstrip() # Loop to read header data while True: l = s.readline() #print('Received Headerdata %s' % l.decode('utf-8')) if not l or l == b"\r\n": break # Header data if l.startswith(b"Transfer-Encoding:"): if b"chunked" in l: # decode added, can't cast implicit from bytes to string raise ValueError("Unsupported " + l.decode('utf-8')) elif l.startswith(b"Location:") and 300 <= status <= 399: if not redir_cnt: raise ValueError("Too many redirects") redir_cnt -= 1 url = l[9:].decode().strip() #print("Redirect to: %s" % url) # set status as signal for loop status = 302 if parse_headers is False: pass elif parse_headers is True: l = l.decode() # print('Headers: %s ' % l) k, v = l.split(":", 1) # adding cookie support (cookies are overwritten as they have the same key in dict) # not supported is the domain attribute of cookies, this is not set # new cookies are added to the supplied cookies if cookies is None: cookies = {} if k == 'Set-Cookie': ck, cv = v.split("=", 1) cookies[ck.strip()] = cv.strip() # else it is not a cookie, just normal header else: resp_d[k] = v.strip() else: parse_headers(l, resp_d) except OSError: s.close() print('Socket closed') raise # if redirect repeat else leave loop if status != 302: break # if redirect false leave loop if (status == 302) and not followRedirect: break # if 302 and redirect = true then loop resp = Response(s) resp.url = url resp.status_code = status resp.reason = reason if resp_d is not None: resp.headers = resp_d # adding cookie support resp.cookies = cookies return resp