本文整理匯總了Python中requests.sessions.Session.post方法的典型用法代碼示例。如果您正苦於以下問題:Python Session.post方法的具體用法?Python Session.post怎麽用?Python Session.post使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類requests.sessions.Session
的用法示例。
在下文中一共展示了Session.post方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: EMDRUploader
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import post [as 別名]
class EMDRUploader(Thread):
def __init__(self, statsCollector):
Thread.__init__(self)
self._queue = Queue()
self.setDaemon(True)
self._session = Session()
self._session.headers.update({
"User-Agent": "CRESTMarketTrawler/{0} ([email protected])".format(VERSION)
})
self._pool = Pool(size=10)
self.statsCollector = statsCollector
def notify(self, regionID, typeID, orders):
self._queue.put((timestampString(), regionID, typeID, orders))
self.statsCollector.tally("emdr_send_queued")
queueSize = self._queue.qsize()
self.statsCollector.datapoint("emdr_queue_size", queueSize)
if queueSize > 100:
logger.error("EMDR submit queue is about {0} items long!".format(queueSize))
elif queueSize > 10:
logger.warn("EMDR submit queue is about {0} items long!".format(queueSize))
def run(self):
def submit(generationTime, regionID, typeID, orders):
uudif = json.dumps(EMDROrdersAdapter(generationTime, regionID, typeID, orders))
res = self._session.post("http://upload.eve-emdr.com/upload/", data=uudif)
self.statsCollector.tally("emdr_sent")
if res.status_code != 200:
logger.error("Error {0} submitting to EMDR: {1}".format(res.status_code, res.content))
self.statsCollector.tally("emdr_errored")
while True:
(generationTime, regionID, typeID, orders) = self._queue.get()
self._pool.spawn(submit, generationTime, regionID, typeID, orders)
示例2: post
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import post [as 別名]
def post(
self,
url,
data=None,
json=None,
**kwargs
):
return Session.post(
self,
self._get_resource_uri(url),
data, json,
**self._set_default_timeout(
**kwargs
)
)
示例3: fetch_data
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import post [as 別名]
def fetch_data(dl_type=None, **kwargs):
"""
Fetch Receipts, Expenditures, and Committees.
dl_type is one of those three choices.
kwargs depend on the choice.
Receipts and Expenditures need start_date and end_date for search.
Committees need a name_start kwarg to pass into the search.
Seems like the maximum that you can get is about 250,000 records at a time.
"""
s = Session()
post_data = {
'__EVENTTARGET': 'ctl00$ContentPlaceHolder1$btnText',
'ctl00$pnlMenu_CollapsiblePanelExtender_ClientState': 'true',
'ctl00$AccordionStateBoardMenu_AccordionExtender_ClientState': '0',
'ctl00$mtbSearch': '',
'ctl00$AccordionPaneStateBoardMenu_content$AccordionMainContent_AccordionExtender_ClientState': '-1',
'hiddenInputToUpdateATBuffer_CommonToolkitScripts': '1',
'__EVENTARGUMENT': '',
'__VIEWSTATEGENERATOR': 'E8D1F59A'
}
if dl_type == 'Receipts':
CONT_GET_PARAMS['RcvDate'] = kwargs['start_date']
CONT_GET_PARAMS['RcvDateThru'] = kwargs['end_date']
url = '%s/DownloadList.aspx?%s' % (BASE_URL, urlencode(CONT_GET_PARAMS))
elif dl_type == 'Committees':
COMM_GET_PARAMS['Name'] = kwargs['name_start']
url = '%s/DownloadList.aspx?%s' % (BASE_URL, urlencode(COMM_GET_PARAMS))
elif dl_type == 'Expenditures':
EXP_GET_PARAMS['ExpendedDate'] = kwargs['start_date']
EXP_GET_PARAMS['ExpendedDateThru'] = kwargs['end_date']
url = '%s/DownloadList.aspx?%s' % (BASE_URL, urlencode(EXP_GET_PARAMS))
elif dl_type == 'Candidates':
url = 'http://www.elections.state.il.us/ElectionInformation/CandDataFile.aspx?id=%s' % kwargs['election_id']
g = s.get(url)
if 'Unexpected errors occurred trying to populate page' in g.content:
return None
soup = BeautifulSoup(g.content)
view_state = soup.find('input', attrs={'id': '__VIEWSTATE'}).get('value')
event_val = soup.find('input', attrs={'id': '__EVENTVALIDATION'}).get('value')
post_data['__VIEWSTATE'] = view_state
post_data['__EVENTVALIDATION'] = event_val
dl_page = s.post(url, data=post_data)
if dl_page.status_code == 200:
return dl_page.content
else:
return None
示例4: EMDRUploader
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import post [as 別名]
class EMDRUploader(Thread):
def __init__(self, statsCollector):
Thread.__init__(self)
self._queue = Queue(EMDR_QUEUE_SIZE)
self.setDaemon(True)
self._session = Session()
self._session.headers.update({
"User-Agent": USER_AGENT_STRING
})
self._pool = Pool(size=10)
self.statsCollector = statsCollector
def notify(self, regionID, orders):
self._queue.put((timestampString(), regionID, orders))
self.statsCollector.tally("emdr_send_queued")
queueSize = self._queue.qsize()
self.statsCollector.datapoint("emdr_queue_size", queueSize)
if queueSize > EMDR_QUEUE_SIZE / 2:
logger.warn("EMDR submit queue is about {0} items long!".format(queueSize))
def run(self):
def submit(generationTime, regionID, orders):
chunks = chunkOrders(orders)
for idx, orderChunk in enumerate(chunks):
with TemporaryFile() as gzfile:
ujson.dump(
EMDROrdersAdapter(generationTime, regionID, orderChunk),
gzip.GzipFile(fileobj=gzfile, mode="wb")
)
headers = {'Content-Length': str(gzfile.tell()),
'Content-Encoding': 'gzip', # what EMDR wants
# 'Transfer-Encoding': 'gzip' # what is strictly true
}
gzfile.seek(0, 0)
logger.info(
"Submitting to EMDR for region {} (chunk {} of {})".format(regionID, idx + 1, len(chunks)))
res = self._session.post("http://upload.eve-emdr.com/upload/", data=gzfile, headers=headers)
self.statsCollector.tally("emdr_chunks_sent")
if res.status_code != 200:
logger.error("Error {0} submitting to EMDR: {1}".format(res.status_code, res.content))
self.statsCollector.tally("emdr_errored")
while True:
(generationTime, regionID, orders) = self._queue.get()
self.statsCollector.datapoint("emdr_queue_size", self._queue.qsize())
self._pool.spawn(submit, generationTime, regionID, orders)
示例5: Api
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import post [as 別名]
class Api(object):
API_URL = 'http://192.168.33.10' # todo: add to config
API_VERSION = 'v1'
def __init__(self, device_key):
self.base_url = '{0}/{1}/'.format(self.API_URL, self.API_VERSION)
self.session = Session()
self.session.auth = KeyAuth(device_key)
self.session.headers.update({
'Content-Type': 'application/json'
})
def request(self, method, url, **kwargs):
"""Constructs and sends a Request to the Pinaple API."""
full_url = urljoin(self.base_url, url)
if 'data' in kwargs:
kwargs['data'] = self._encode_data(kwargs['data'])
return super(Api, self).request(method, full_url, **kwargs)
def _encode_data(self, data, **kwargs):
"""Returns data encoded as JSON using a custom encoder."""
encoder = JSONEncoder(**kwargs) if kwargs else self._json_encoder
return encoder.encode(data)
def test(self):
url = urljoin(self.base_url, 'functions/test')
response = self.session.get(url)
return response
def login(self):
url = urljoin( self.base_url, 'login' )
response = self.session.post( url )
if response.status_code is not 200:
print('[error] device is not authorized')
exit()
data = response.json()
self.session.auth = SessionAuth(data['session_token'])