Twisted post length limit - http

How do you set the limit on the length of a POST request in twisted? I've looked around in the docs and can't find anything. It would even help if I knew the default limit.

I don't believe there is a default limit on the size of incoming POST data, but you could pretty easily impose one by overriding the Request.handleContentChunk() method, with something like:
from twisted.web import server
class SizeLimitingRequest(server.Request):
def handleContentChunk(self, data):
if self.content.tell() + len(data) > self.size_limit_on_post_data:
raise SomeKindOfError("too much data nooooooo")
return server.Request.handleContentChunk(self, data)
the actual method you'd be overriding is in twisted.web.http.Request, which is a superclass of server.Request. To make use of your shiny new class, just set your Site instance's requestFactory attribute:
mysite.requestFactory = SizeLimitingRequest

Related

LocustIO: How to do batch request

I started to use LocustIO for load testing a 3rd party API which provides a way to do batch requests (http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_BatchRequests).
How can this be done using LocustIO?
I tried with the following:
def batch(self):
response = self.client.request(method="POST", url="/$batch", auth=("ABC", "DEF"), headers={"ContentType": "multipart/mixed; boundary=batch_36522ad7-fc75-4b56-8c71-56071383e77b"}, data="Content-Type: application/http\nContent-Transfer-Encoding: binary\n\nGET putyoururlhere HTTP/1.1\nAccept: application/json\n\n\n")
Auth is something I need to have authentication to the API, but that's not the point of the question and "putyoururlhere" should be replaced with the actual url. Either way, it gives errors when executing the test, so I must be doing something wrong.
People with experience how to do this?
Kind regards!
The data parameter should be your POST body (only), you cant put additional headers in it the way you did. You probably just want to add them as additional entries in the dict you pass as headers
Se the documentation for python requests library for more details. https://requests.readthedocs.io/en/master/

Whats the difference between 'options' and 'overrides' in Rblpapi?

In the documentation here, Bloomberg does not make a distinction in the request. The requests can only have 3 things:securities, fields and overrides.
So what are options? How do they get used? Is this a distinction imposed by Rblpapi? Can someone explain the distinction?
Please let me know if I am incorrectly understanding something.
Options are parameters that change how a Request or Subscription should behave. For example, a ref data request with returnEID=true will return the EID(s) of each security in response messages. A Subscription with interval=5.0 will make it an Intervalized Subscription.
Overrides, on the other hand, are field/value pairs that you specify in Requests to alter the form or content of the returned fields, for example, GICS_SECTOR_NAME will normally return sector name in English (or precisely the default terminal language), you can specify SECURITY_NAME_LANG=9 override to get the name in Korean. You can also "request" SECURITY_NAME_LANG field to know the language used in GICS_SECTOR_NAME field. Overrides can be used in Request/Response only (not subscriptions), and are applied to the entire request, on all fields that react to that override.
option.names = "optName", option.values = "optVal"
in R, maps to:
request.set("optName", optVal);
in Java. E.g:
option.names="periodicitySelection", option.values="MONTHLY")
request.set("periodicitySelection", "MONTHLY");

Optimizing Get Requests - Changing RoundTripper with Transport go

I reach a limit in the number of requests I can get using the default httpClient that my API wrapper provides.
//I was getting something like this at the beginning
Head www.example.com:80/: lookup example.com: no such host
To solve this I want to change the MaxIdleConnsPerHost setting for the httpClient.Transport of my client. It looks much more like this:
However, the Transport my client uses is a RoundTripper and subsequently, it doesn't have a MaxIdleConnsPerHost param.
&oauth2.Transport{Source:(*oauth2.reuseTokenSource)(0xc2082ac0c0),
Base:http.RoundTripper(nil),
mu:sync.Mutex{state:0, sema:0x0},
modReq:map[*http.Request]*http.Request(nil)
}
The one I'm providing is mostly a default one and it lacks the proper configuration I suppose or simple what I want to do is not feasible.
&http.Transport{idleMu:sync.Mutex{state:0, sema:0x0},
idleConn:map[http.connectMethodKey][]*http.persistConn(nil),
idleConnCh:map[http.connectMethodKey]chan *http.persistConn(nil),
reqMu:sync.Mutex{state:0, sema:0x0},
reqCanceler:map[*http.Request]func()(nil),
altMu:sync.RWMutex{w:sync.Mutex{state:0, sema:0x0},
writerSem:0x0,
readerSem:0x0,
readerCount:0,
readerWait:0},
altProto:map[string]http.RoundTripper(nil),
Proxy:(func(*http.Request) (*url.URL, error))(nil),
Dial:(func(string, string) (net.Conn, error))(nil),
TLSClientConfig:(*tls.Config)(nil),
TLSHandshakeTimeout:0,
DisableKeepAlives:false,
DisableCompression:false,
MaxIdleConnsPerHost:200,
ResponseHeaderTimeout:0}
Can someone guide me on the right direction?

Invalidate/prevent memoize with plone.memoize.ram

I've and Zope utility with a method that perform network processes.
As the result of the is valid for a while, I'm using plone.memoize.ram to cache the result.
MyClass(object):
#cache(cache_key)
def do_auth(self, adapter, data):
# performing expensive network process here
...and the cache function:
def cache_key(method, utility, data):
return time() // 60 * 60))
But I want to prevent the memoization to take place when the do_auth call returns empty results (or raise network errors).
Looking at the plone.memoize code it seems I need to raise ram.DontCache() exception, but before doing this I need a way to investigate the old cached value.
How can I get the cached data from the cache storage?
I put this together from several code I wrote...
It's not tested but may help you.
You may access the cached data using the ICacheChooser utility.
It's call method needs the dotted name to the function you cached, in your case itself
key = '{0}.{1}'.format(__name__, method.__name__)
cache = getUtility(ICacheChooser)(key)
storage = cache.ramcache._getStorage()._data
cached_infos = storage.get(key)
In cached_infos there should be all infos you need.

How to track custom parameter with Google Analytics and utm.gif

The situation
I'd like to use GA to track some serverside operations. That's why I cant make use of the GA JavaScript functions. But, as you might know, you can request the utm.gif right from your server. This already works fine.
The Problem
I'd like to trackt custom parameters. But I have no idea how to add them in the right format to the url-request
This one should do the custom parms. But I didn't get any results in GA.
utme=5(Init*load_success*http://www.mydomain.de)8(userstatus)9(fan)11(2)
Full list of params:
ref ts
utmac UA-XXXXXX-5
utmcc __utma=186215409.1789216404.1265552708.1280074861.1280493144.21;+__utmz=;
utmcs ISO-8859-1
utmdt Button
utme 5(Init*load_success*http://www.mydomain.de)8(mycustomvar)9(mycustomvalue)11(2)
utmfl -
utmhn mydomain.de
utmje -
utmn 1114675642
utmp button
utmr http://www.mydomain.de
utmsc -
utmsr -
utmul de-de
utmwv 4.5.7
not sure what's going wrong, given what you posted, but how about you write out what you want to send the traditional way (with javascript) and put it on a test page. Use firebug or whatever to grab the requested url that's built and compare it to what you have now.
The value of the utme gif Request parameter is encoded by ga.js--it's the only one that is, as far as i know.
Calling __trackEvent is the usual way to set the value of utme. These are client-side events though, which is no doubt why you are trying to set utme directly.
So if you just want to bind 5(Initload_successhttp://www.mydomain.de)8(userstatus)9(fan)11(2) to the variable utme, and you can't rely on user-behavior to trigger that binding, then here's what i suggest:
Pack your data into a 'custom variable' scoped to the page--this way, when the __trackPageview() is called, the value will be set.
Here's the analytics code required in your HTML to implement that:
The method signature for a custom variable:
pageTracker._setCustomVar(slot, // integer between 1 and 5, inclusive (just use '1')
name, // user-defined name for the custom variable
value, // string representing the value for the custom variable
scope, // you want '3' for page-level (an int, not a string though)
);
Within the HTML (order matter, of course):
pageTracker.__setCustomvar(1, "A Name", "A Value", 3);
pageTracker.__trackPageview();
A key point here is that the parameter 'value' can be set dynamically, so for the 'value' parameter, i guess you want to pass in 5(Initload_successhttp://www.mydomain.de)8(userstatus)9(fan)11(2)
Finally, here are the two key sources (Implementation Guide, Usage Guide) on Custom Variables from the GA Team

Resources