def test_cookies(self): url = 'http://httpbin.org/cookies/set' http_cookie = URLFetch(url=url, session=self.session) try: resp = http_cookie(var1=1, var2='a') except requests.exceptions.ConnectionError as e: raise requests.exceptions.ConnectionError( 'Error fetching (check proxy settings):', url) rjson = resp.json() ok_cookie = 0 for cookie in self.session.cookies: if cookie.name == 'var1' and cookie.value == '1': ok_cookie += 1 if cookie.name == 'var2' and cookie.value == 'a': ok_cookie += 1 self.assertEqual(ok_cookie, 2) url = 'http://httpbin.org/get' http_get = URLFetch(url=url, session=self.session) try: resp = http_get(key1='val1', key2='val2') except requests.exceptions.ConnectionError as e: raise requests.exceptions.ConnectionError( 'Error fetching (check proxy settings):', url) rjson = resp.json() self.assertGreaterEqual(rjson['headers']['Cookie'].find('var1=1'), 0)
def test_headers(self): url = 'http://httpbin.org/get' http_get = URLFetch(url=url, session=self.session) try: resp = http_get(key1='val1', key2='val2') except requests.exceptions.ConnectionError as e: raise requests.exceptions.ConnectionError( 'Error fetching (check proxy settings):', url) json = resp.json() self.assertEqual(json['headers']['Host'], 'httpbin.org') self.assertEqual(json['headers']['User-Agent'], 'Testing')
def test_post(self): url = 'http://httpbin.org/post' http_post = URLFetch(url=url, method='post', session=self.session) try: resp = http_post(key1='val1', key2='val2') except requests.exceptions.ConnectionError as e: raise requests.exceptions.ConnectionError( 'Error fetching (check proxy settings):', url) rjson = resp.json() self.assertEqual(rjson['form']['key1'], 'val1') self.assertEqual(rjson['form']['key2'], 'val2')
def test_json(self): url = 'http://httpbin.org/post' http_get = URLFetch(url=url, method='post', json=True, session=self.session) try: resp = http_get(key1='val1', key2='val2') except requests.exceptions.ConnectionError as e: raise requests.exceptions.ConnectionError( 'Error fetching (check proxy settings):', url) rjson = resp.json() self.assertEqual(json.loads(rjson['data']), { u'key1': u'val1', u'key2': u'val2' })
headers = { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, sdch, br', 'Accept-Language': 'en-GB,en-US;q=0.8,en;q=0.6', 'Connection': 'keep-alive', 'Host': 'www.nseindia.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest' } """ 1. Stock symbol 2. Series eg. EQ, N1 ... """ quote_eq_url = URLFetch( url= 'https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/ajaxGetQuoteJSON.jsp?symbol=%s&series=%s', headers=headers) """ 1. Underlying security (stock symbol or index name) 2. instrument (FUTSTK, OPTSTK, FUTIDX, OPTIDX) 3. expiry (ddMMMyyyy) 4. type (CE/PE for options, - for futures 5. strike (strike price upto two decimal places """ quote_derivative_url = URLFetch( url= 'https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/ajaxFOGetQuoteJSON.jsp?underlying=%s&instrument=%s&expiry=%s&type=%s&strike=%s', headers=headers) """ 1. Underlying symbol 2. instrument (FUTSTK, OPTSTK, FUTIDX, OPTIDX)
headers = { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, sdch, br', 'Accept-Language': 'en-GB,en-US;q=0.8,en;q=0.6', 'Connection': 'keep-alive', 'Host': 'www.nseindia.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest' } """ 1. Stock symbol 2. Series eg. EQ, N1 ... """ quote_eq_url = URLFetch( url= 'https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=%s&series=%s', headers=headers) """ 1. Underlying security (stock symbol or index name) 2. instrument (FUTSTK, OPTSTK, FUTIDX, OPTIDX) 3. expiry (ddMMMyyyy) 4. type (CE/PE for options, - for futures 5. strike (strike price upto two decimal places """ quote_derivative_url = URLFetch( url= 'https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=%s&instrument=%s&expiry=%s&type=%s&strike=%s', headers=headers) """ 1. Underlying symbol 2. instrument (FUTSTK, OPTSTK, FUTIDX, OPTIDX)
headers = { 'Accept': '*/*', 'Accept-Language': 'en-US,en;q=0.5', 'Host': 'nseindia.com', 'Referer': 'http://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=INFY&illiquid=0', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0', 'X-Requested-With': 'XMLHttpRequest' } """ 1. Stock symbol 2. Series eg. EQ, N1 ... """ quote_eq_url = URLFetch( url= 'https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=%s', headers=headers) quote_fx_url = URLFetch( 'https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteCID.jsp?underlying=%s&instrument=FUTCUR&expiry=%s&key=FUTCUR%s%s--%s', headers=headers) """ 1. Underlying security (stock symbol or index name) 2. instrument (FUTSTK, OPTSTK, FUTIDX, OPTIDX) 3. expiry (ddMMMyyyy) 4. type (CE/PE for options, - for futures 5. strike (strike price upto two decimal places """ quote_derivative_url = URLFetch( url= 'https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=%s&instrument=%s&expiry=%s&type=%s&strike=%s',
# -*- coding: utf-8 -*- """ Created on Fri Dec 18 19:00:45 2015 @author: SW274998 """ from nsepy.commons import URLFetch from requests import Session from functools import partial from nsepy.constants import symbol_count, symbol_list headers = {'Accept' : '*/*', 'Accept-Language' : 'en-US,en;q=0.5', 'Host': 'nseindia.com', 'Referer': 'http://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=INFY&illiquid=0', 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0', 'X-Requested-With': 'XMLHttpRequest' } get_quote = URLFetch(url = 'http://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp', headers=headers)