Best Python code snippet using slash
_useragent.py
Source:_useragent.py
...129 else:130 del want[scheme] # already got it131 # add the scheme handlers that are missing132 for scheme in want.keys():133 self._set_handler(scheme, True)134 def set_cookiejar(self, cookiejar):135 """Set a mechanize.CookieJar, or None."""136 self._set_handler("_cookies", obj=cookiejar)137 # XXX could use Greg Stein's httpx for some of this instead?138 # or httplib2??139 def set_proxies(self, proxies=None, proxy_bypass=None):140 """Configure proxy settings.141 proxies: dictionary mapping URL scheme to proxy specification. None142 means use the default system-specific settings.143 proxy_bypass: function taking hostname, returning whether proxy should144 be used. None means use the default system-specific settings.145 The default is to try to obtain proxy settings from the system (see the146 documentation for urllib.urlopen for information about the147 system-specific methods used -- note that's urllib, not urllib2).148 To avoid all use of proxies, pass an empty proxies dict.149 >>> ua = UserAgentBase()150 >>> def proxy_bypass(hostname):151 ... return hostname == "noproxy.com"152 >>> ua.set_proxies(153 ... {"http": "joe:password@myproxy.example.com:3128",154 ... "ftp": "proxy.example.com"},155 ... proxy_bypass)156 """157 self._set_handler("_proxy", True,158 constructor_kwds=dict(proxies=proxies,159 proxy_bypass=proxy_bypass))160 def add_password(self, url, user, password, realm=None):161 self._password_manager.add_password(realm, url, user, password)162 def add_proxy_password(self, user, password, hostport=None, realm=None):163 self._proxy_password_manager.add_password(164 realm, hostport, user, password)165 def add_client_certificate(self, url, key_file, cert_file):166 """Add an SSL client certificate, for HTTPS client auth.167 key_file and cert_file must be filenames of the key and certificate168 files, in PEM format. You can use e.g. OpenSSL to convert a p12 (PKCS169 12) file to PEM format:170 openssl pkcs12 -clcerts -nokeys -in cert.p12 -out cert.pem171 openssl pkcs12 -nocerts -in cert.p12 -out key.pem172 Note that client certificate password input is very inflexible ATM. At173 the moment this seems to be console only, which is presumably the174 default behaviour of libopenssl. In future mechanize may support175 third-party libraries that (I assume) allow more options here.176 """177 self._client_cert_manager.add_key_cert(url, key_file, cert_file)178 # the following are rarely useful -- use add_password / add_proxy_password179 # instead180 def set_password_manager(self, password_manager):181 """Set a mechanize.HTTPPasswordMgrWithDefaultRealm, or None."""182 self._password_manager = password_manager183 self._set_handler("_basicauth", obj=password_manager)184 self._set_handler("_digestauth", obj=password_manager)185 def set_proxy_password_manager(self, password_manager):186 """Set a mechanize.HTTPProxyPasswordMgr, or None."""187 self._proxy_password_manager = password_manager188 self._set_handler("_proxy_basicauth", obj=password_manager)189 self._set_handler("_proxy_digestauth", obj=password_manager)190 def set_client_cert_manager(self, cert_manager):191 """Set a mechanize.HTTPClientCertMgr, or None."""192 self._client_cert_manager = cert_manager193 handler = self._ua_handlers["https"]194 handler.client_cert_manager = cert_manager195 # these methods all take a boolean parameter196 def set_handle_robots(self, handle):197 """Set whether to observe rules from robots.txt."""198 self._set_handler("_robots", handle)199 def set_handle_redirect(self, handle):200 """Set whether to handle HTTP 30x redirections."""201 self._set_handler("_redirect", handle)202 def set_handle_refresh(self, handle, max_time=None, honor_time=True):203 """Set whether to handle HTTP Refresh headers."""204 self._set_handler("_refresh", handle, constructor_kwds=205 {"max_time": max_time, "honor_time": honor_time})206 def set_handle_equiv(self, handle, head_parser_class=None):207 """Set whether to treat HTML http-equiv headers like HTTP headers.208 Response objects may be .seek()able if this is set (currently returned209 responses are, raised HTTPError exception responses are not).210 """211 if head_parser_class is not None:212 constructor_kwds = {"head_parser_class": head_parser_class}213 else:214 constructor_kwds={}215 self._set_handler("_equiv", handle, constructor_kwds=constructor_kwds)216 def set_handle_gzip(self, handle):217 """Handle gzip transfer encoding.218 """219 if handle:220 warnings.warn(221 "gzip transfer encoding is experimental!", stacklevel=2)222 self._set_handler("_gzip", handle)223 def set_debug_redirects(self, handle):224 """Log information about HTTP redirects (including refreshes).225 Logging is performed using module logging. The logger name is226 "mechanize.http_redirects". To actually print some debug output,227 eg:228 import sys, logging229 logger = logging.getLogger("mechanize.http_redirects")230 logger.addHandler(logging.StreamHandler(sys.stdout))231 logger.setLevel(logging.INFO)232 Other logger names relevant to this module:233 "mechanize.http_responses"234 "mechanize.cookies"235 To turn on everything:236 import sys, logging237 logger = logging.getLogger("mechanize")238 logger.addHandler(logging.StreamHandler(sys.stdout))239 logger.setLevel(logging.INFO)240 """241 self._set_handler("_debug_redirect", handle)242 def set_debug_responses(self, handle):243 """Log HTTP response bodies.244 See docstring for .set_debug_redirects() for details of logging.245 Response objects may be .seek()able if this is set (currently returned246 responses are, raised HTTPError exception responses are not).247 """248 self._set_handler("_debug_response_body", handle)249 def set_debug_http(self, handle):250 """Print HTTP headers to sys.stdout."""251 level = int(bool(handle))252 for scheme in "http", "https":253 h = self._ua_handlers.get(scheme)254 if h is not None:255 h.set_http_debuglevel(level)256 def _set_handler(self, name, handle=None, obj=None,257 constructor_args=(), constructor_kwds={}):258 if handle is None:259 handle = obj is not None260 if handle:261 handler_class = self.handler_classes[name]262 if obj is not None:263 newhandler = handler_class(obj)264 else:265 newhandler = handler_class(266 *constructor_args, **constructor_kwds)267 else:268 newhandler = None269 self._replace_handler(name, newhandler)270 def _replace_handler(self, name, newhandler=None):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!