I'm searching for something similar to Python's pickle. I want to do like this example:
https://www.quora.com/Is-there-a-way-to-keep-the-session-after-login-with-Selenium-Python
saving cookies
import pickle
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.quora.com')
# login code
pickle.dump(driver.get_cookies() , open("QuoraCookies.pkl","wb"))
loading cookies
import pickle
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.quora.com')
for cookie in pickle.load(open("QuoraCookies.pkl", "rb")):
driver.add_cookie(cookie)
I've tried many examples from the web without success. I saw the topics here as well.
In groovy case I'll :
Set<Cookie> allCookies = driver.manage().getCookies();
Variable allCookies should be serialized to file and deserialized later.
Java serialization is an option here. With credit to this post, here is an implementation:
import org.openqa.selenium.Cookie
def loadFile = { filename ->
def result = null
try {
def fis = new FileInputStream(filename)
def instream= new ObjectInputStream(fis)
result = (Set<Cookie>) instream.readObject()
instream.close()
} catch (Exception e) {
System.out.println(e)
}
return result
}
def saveFile = { set, filename ->
try {
def fos = new FileOutputStream(filename)
def out = new ObjectOutputStream(fos)
out.writeObject(set)
out.flush()
out.close()
} catch (IOException e) {
System.out.println(e)
}
}
and a sample driver program:
// ----------------------- main
if (args.size() >= 2) {
def action = args[0]
def file = args[1]
if (action == "write") {
def cookies = new HashSet<Cookie>()
cookies << new Cookie("Toronto", "Canada")
cookies << new Cookie("London", "UK")
cookies << new Cookie("Paris", "France")
saveFile(cookies, file)
} else if (action == "read") {
def cookies = loadFile(file)
cookies.each { cookie ->
println "${cookie.name} ${cookie.value}"
}
} else {
println "Unknown action: " + action
}
} else {
println "Usage: groovy CookieManager.groovy [read|write] filename"
}
Example usage (a) with all above code in the same script and (b) assuming that client-combined-3.0.0-beta2-nodeps.jar from here is on the classpath:
bash$ groovy CookieManager.groovy write set.dat
bash$ groovy CookieManager.groovy read set.dat
London UK
Toronto Canada
Paris France
Related
For the below response I need to fetch the rideId and pass it to the next request in Jmeter.Also ,the API that generates below response should be executed until the eventType is HANDSHAKE.
[{"id":90856,"eventType":"HANDSHAKE","parameters":"{\"handshakeExpiration\":1669217518986,\"rideId\":3107}"}]
I am using the code :
import groovy.json.JsonSlurper;
def jsonSlurper=new JsonSlurper();
def apiDetailsArr=jsonSlurper.parse(prev.getResponseData())
def apiDetails=apiDetailsArr.size()>0?apiDetailsArr.get(0):null
def shouldRun = "1"
if(apiDetails!=null)
{
log.info("details",apiDetails.eventType+"")
if(apiDetails.eventType="HANDSHAKE"){
shouldRun="0"
}
def object=jsonSlurper.parseText(apiDetails.parameters)
log.info("xyz",object+"")
def id=object.rideId;
log.info("id",id+"")
vars.put("id", id+"")
}
else{
shouldRun="1"`enter code here`
}
`Condition for while controller : `${__javaScript( "${shouldRun}" != "0",)}``
All your log.info function calls cause syntax errors, you can see it yourself in (surprise!) the jmeter.log file
I fail to see where you're assigning shouldRun JMeter Variable which is used in the While Controller
Suggested code change:
import groovy.json.JsonSlurper;
def jsonSlurper = new JsonSlurper();
def apiDetailsArr = jsonSlurper.parse(prev.getResponseData())
def apiDetails = apiDetailsArr.size() > 0 ? apiDetailsArr.get(0) : null
def shouldRun = "1"
if (apiDetails != null) {
log.info("details", apiDetails.eventType + "")
if (apiDetails.eventType = "HANDSHAKE") {
shouldRun = "0"
}
def object = jsonSlurper.parseText(apiDetails.parameters)
log.info("xyz" + object)
def id = object.rideId;
log.info("id" + id)
vars.put("id", id as String)
} else {
shouldRun = "1"
}
vars.put("shouldRun", shouldRun)
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?
Need to send an LDAP search request with message ID set to 0 value (as part of RFC validation testing). Tried the following modified code from apache directory api examples section:
import java.io.IOException;
import org.apache.directory.api.ldap.model.entry.DefaultEntry;
import org.apache.directory.api.ldap.model.entry.ModificationOperation;
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.apache.directory.api.ldap.model.name.Dn;
import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
import org.apache.directory.api.ldap.model.message.SearchRequest;
import org.apache.directory.api.ldap.model.message.SearchRequestImpl;
import org.apache.directory.api.ldap.model.cursor.SearchCursor;
import org.apache.directory.api.ldap.model.exception.LdapNoPermissionException;
import org.apache.directory.api.ldap.model.name.Dn;
import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
public class ManageLDAPConnection {
private static Dn getSafeSearchBaseDn(String dn) throws LdapInvalidDnException{
Dn searchBaseDn = null;
if (dn != null && !dn.isEmpty()){
searchBaseDn = new Dn(dn);
}else{
searchBaseDn = Dn.ROOT_DSE;
}
return searchBaseDn;
}
public static void main (String[] args) {
int messageId = 0;
int port = 389;
String username = "<Admin CN>";
String password = "<Password>";
String hostname = "<IP>";
SearchCursor searchResult = null;
String dn = "<DN>";
String filterExpr = "(objectclass=*)";
org.apache.directory.api.ldap.model.message.SearchScope searchScopeValue = org.apache.directory.api.ldap.model.message.SearchScope.OBJECT;
LdapConnection connection = new LdapNetworkConnection(hostname, port);
try {
connection.bind(username, password);
System.out.println("Connected successfully");
} catch (LdapException e) {
System.out.println("Unable to bind");
}
try {
SearchRequest searchRequest = new SearchRequestImpl();
System.out.println(searchRequest.getMessageId());
searchRequest.setMessageId(0);
System.out.println(searchRequest.getMessageId());
searchRequest.setBase(getSafeSearchBaseDn(dn));
searchRequest.setFilter(filterExpr);
searchRequest.setScope(searchScopeValue);
searchResult = connection.search(searchRequest);
} catch (LdapNoPermissionException e){
System.out.println("No permission exception");
} catch (LdapException e){
System.out.println("LDAP Exception: " + e.getMessage());
}
}
}
The above code is able to send the request, but the message ID is still sent as non zero,
even though the following has been done:
searchRequest.setMessageId(0);
You're clearly going to have to use a different library, or modify this one, or go to a lower level. It isn't at all surprising that this library prevents you from shooting yourself in the foot.
Had some solution in python's pyasn1-modules. The following seems to work well:
from pyasn1.type import univ, namedval, namedtype, tag
from pyasn1.codec.ber import encoder
import socket
from pyasn1_modules.rfc2251 import *
ldap_bind_request = BindRequest()
ldap_bind_request.setComponentByName('version', 3)
ldap_bind_request.setComponentByName('name', 'cn=admin,o=org')
ldap_auth = AuthenticationChoice()
ldap_auth.setComponentByName('simple', 'mypwd')
ldap_bind_request.setComponentByName('authentication', ldap_auth)
ldap_message = LDAPMessage()
ldap_message.setComponentByName('messageID', 0)
ldap_message.setComponentByName('protocolOp', ldap_bind_request)
print(ldap_bind_request.prettyPrint())
print(dir(ldap_bind_request))
encoded_request = encoder.encode(ldap_message)
print(encoded_request)
asock = socket.socket()
asock.connect(('127.0.0.1', 389))
asock.send(encoded_request)
There is something named JAVA ASN.1 Compiler (JAC). Trying to see if they provide something similar, with less of object oriented complexity which is common in java :)
I am using Google App Engine (python) and have a data: url of a PNG image available on the server. The PNG image was never in a file, as it was generated from some canvas code using toDataUrl() and ajaxed to the server. I would like to allow the user to click a button and be able to select a filename and save the PNG image locally. The Save As dialog box would supply a default filename.png. The target browser is FireFox. I have supplied sample code that doesn't work. There are several questions on stackoverflow that are somewhat like this one, but each is a little different.
I am setting content-disposition as attachment with a suggested filename. I set the header content-type to application/octet-stream. But I don't get the SaveAs dialog. What am I missing?
The app.yaml file is the standard
application: saveas
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: main.py
The index.html is as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script>
function saveAsPng()
{
var alldata;
var httpRequest;
var response;
var myheaders;
httprequest = new XMLHttpRequest();
if ( !httprequest )
{
alert("In saveAsPng, XMLHttpRequest failed.");
return;
}
/* Make this a json string */
alldata = JSON.stringify("no data");
try
{
httprequest.open('POST', '/handlebitmap', true);
httprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httprequest.setRequestHeader("Content-length", alldata.length);
httprequest.setRequestHeader("Connection", "close");
httprequest.onreadystatechange = function()
{
if ( httprequest.readyState == 4 )
{
if (httprequest.status == 200)
{
/* a status of 200 is good. */
response = null;
try
{
response = JSON.parse(httprequest.responseText);
}
catch (e)
{
response = httprequest.responseText;
}
if ( response == "error" )
{
alert("In saveAsPng callback, response == error");
return false;
}
else
{
/* This is the successful exit. */
//alert("response = " +response);
window.location.href = response;
return true;
}
}
else
{
/* httprequest.status was not 200, so must be an error. */
alert("saveAsPNG callback, status = " +httprequest.status);
return false;
}
} /* End of if where readyState was 4. */
} /* End of the callback function */
/* Make the actual request */
httprequest.send(alldata);
}
catch(e)
{
alert("In saveAsPng, Can't connect to the server");
}
} /* End of the saveAsPng function */
The python code is as follows:
# !/usr/bin/env python
import os
import base64
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
class MainPage(webapp.RequestHandler):
""" Renders the main template."""
def get(self):
template_values = { 'title':'Test Save As PNG', }
path = os.path.join(os.path.dirname(__file__), "index.html")
self.response.out.write(template.render(path, template_values))
class BitmapHandler(webapp.RequestHandler):
""" Shows the Save As with a default filename. """
def post(self):
origdata = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
urldata = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
decodeddata = base64.b64decode(urldata)
self.response.headers['Content-Type'] = 'application/octet-stream'
self.response.headers['Content-Disposition'] = 'attachment; filename="untitled.png"'
self.response.out.write(decodeddata)
def main():
app = webapp.WSGIApplication([
('/', MainPage),
('/handlebitmap',BitmapHandler),
], debug=True)
util.run_wsgi_app(app)
if __name__ == '__main__':
main()
{{title}}
Perhaps you want to change the "def post" to a "def get"? I just tried your sample and it seemed to be working as expected after that change (only tried /handlebitmap)
I wish to have a file upload form that in addition to the file selection input , also has other input fields like textarea, dropdown etc. The problem is that I cannot access any post parameters other than the file in my blobstore upload handler. I am using the following function call to get the parameter name but it always returns an empty screen.
par = self.request.get("par")
I found another question with a similar problem Uploading a video to google app engine blobstore. The answer to that question suggests a workaround solution to set the filename to the parameter you wish to read which is not sufficient for my needs. Is there a way to access other form parameters in the post method of blobstore upload handler?
Did you find the solution?
In my experience, when using form/multipart request doesn't include the other parameters and they have to be dug out manually.
This is how I dig out parameters out of request that is used to send a file.
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
// for reading form data when posted with multipart/form-data
import java.io.*;
import javax.servlet.ServletException;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.google.appengine.api.datastore.Blob;
// Fetch the attributes for a given model using rails conventions.
// We need to do this in Java because getParameterMap uses generics.
// We currently only support one lever: foo[bar] but not foo[bar][baz].
// We currently only pull the first value, so no support for checkboxes
public class ScopedParameterMap {
public static Map params(HttpServletRequest req, String model)
throws ServletException, IOException {
Map<String, Object> scoped = new HashMap<String, Object>();
if (req.getHeader("Content-Type").startsWith("multipart/form-data")) {
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(req); // this is used to get those params
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
String attr = item.getFieldName();
if (attr.startsWith(model + "[") && attr.endsWith("]")) { // fetches all stuff like article[...], you can modify this to return only one value
int len = 0;
int offset = 0;
byte[] buffer = new byte[8192];
ByteArrayOutputStream file = new ByteArrayOutputStream();
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
offset += len;
file.write(buffer, 0, len);
}
String key = attr.split("\\[|\\]")[1];
if (item.isFormField()) {
scoped.put(key, file.toString());
} else {
if (file.size() > 0) {
scoped.put(key, file.toByteArray());
}
}
}
}
} catch (Exception ex) {
throw new ServletException(ex);
}
} else {
Map params = req.getParameterMap();
Iterator i = params.keySet().iterator();
while (i.hasNext()) {
String attr = (String) i.next();
if (attr.startsWith(model + "[") && attr.endsWith("]")) {
String key = attr.split("\\[|\\]")[1];
String val = ((String[]) params.get(attr))[0];
scoped.put(key, val);
// TODO: when multiple values, set a List instead
}
}
}
return scoped;
}
}
I hope this speedy answer helps, let me know if you have questions.
are there any good examples on how to use Google App Engine from Silverlight, preferably without writing custom webservices?
Cheers
Nik
Here is my approach based heavily on Scott Seely's post
Simply passes XML around, .xap is also served by GAE. I only just got this working yesterday so it is still work in progress.
Google:
app.yaml
application: wowbosscards
version: 1
runtime: python
api_version: 1
handlers:
- url: /WowBossCards.html
static_files: WowBossCards.html
upload: WowBossCards.html
mime_type: text/html
- url: /clientaccesspolicy.xml
static_files: clientaccesspolicy.xml
upload: clientaccesspolicy.xml
mime_type: text/xml
- url: /WowBossCards.xap
static_files: WowBossCards.xap
upload: WowBossCards.xap
mime_type: application/x-silverlight-app
- url: .*
script: wowbosscards.py
wowbosscards.py
#!/usr/bin/env python
import cgi
import datetime
import wsgiref.handlers
import os
import logging
import string
import urllib
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class Xml(db.Model):
xmlContent = db.TextProperty()
date = db.DateTimeProperty(auto_now_add=True)
class XmlCrud(webapp.RequestHandler):
def get(self, xmlType):
xmlKey = string.capitalize(xmlType)
xml = Xml.get_by_key_name(xmlKey)
self.response.headers['Content-Type'] = "application/xml"
self.response.out.write(xml.xmlContent)
def post(self, xmlType):
logging.debug("XmlCrud POST")
xmlKey = string.capitalize(xmlType)
xml = Xml.get_by_key_name(xmlKey)
if len(self.request.get('content')) > 0 :
xml.xmlContent = self.request.get('content')
else:
xml.xmlContent = self.request.body
xml.put()
self.redirect('/upload/' + xmlType)
def main():
Xml.get_or_insert("Bosses", xmlContent="<a>Bosses go here</a>")
Xml.get_or_insert("Dungeons", xmlContent="<a>Dungeons go here</a>")
application = webapp.WSGIApplication([
(r'/xml/(.*)', XmlCrud),
], debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()
Silverlight:
private class RequestExtended
{
public VoidCall CallBack;
public WebRequest Request;
public Uri Host;
public RequestExtended(WebRequest request, VoidCall callBack, Uri host)
{
Request = request;
CallBack = callBack;
Host = host;
}
}
public static void ImportFromGoogle(Uri host, VoidCall callBack)
{
try
{
if (host.Host == "localhost")
{
host = new Uri(host.Scheme + #"://" + host.Host + ":8080");
}
var bossesCrud = new Uri(host, "/xml/bosses");
var bossesRequest = WebRequest.Create(bossesCrud);
bossesRequest.BeginGetResponse(BossesResponse, new RequestExtended(bossesRequest, callBack, host));
}
catch (Exception ex)
{
}
}
public static void BossesResponse(IAsyncResult result)
{
var requestExtended = result.AsyncState as RequestExtended;
try
{
WebResponse response = requestExtended.Request.EndGetResponse(result);
Stream responseStream = response.GetResponseStream();
byte[] bytes = new byte[response.ContentLength];
responseStream.Read(bytes, 0, bytes.Length);
responseStream.Close();
var enc = new System.Text.UTF8Encoding();
string xml = enc.GetString(bytes, 0, bytes.Length);
bosses = GetCollectionFromXmlString<BossCollection>(xml);
if (requestExtended.CallBack != null) requestExtended.CallBack();
}
catch (WebException we)
{
HttpWebResponse response = (HttpWebResponse)we.Response;
response.Close();
}
catch (Exception e)
{
}
}
public static void SaveBossesToGoogle(Uri host)
{
if (host.Host == "localhost")
{
host = new Uri(host.Scheme + #"://" + host.Host + ":8080");
}
var bossesCrud = new Uri(host, "/xml/bosses");
var request = WebRequest.Create(bossesCrud);
request.Method = "POST";
request.ContentType = "text/xml"; //"application/x-www-form-urlencoded";
request.BeginGetRequestStream(GetSaveBossesRequestStreamCallback, new RequestExtended(request, null, host));
}
static void GetSaveBossesRequestStreamCallback(IAsyncResult result)
{
var requestExtended = result.AsyncState as RequestExtended;
try
{
Stream stream = requestExtended.Request.EndGetRequestStream(result);
var xmlSerializer = new XmlSerializer(typeof(BossCollection));
var xmlText = new StringBuilder();
using (TextWriter textWriter = new StringWriter(xmlText))
{
xmlSerializer.Serialize(textWriter, Store.Bosses);
textWriter.Close();
}
var enc = new System.Text.UTF8Encoding();
var bytes = enc.GetBytes(xmlText.ToString());
stream.Write(bytes, 0, bytes.Length);
stream.Close();
requestExtended.Request.BeginGetResponse(SaveResponse, requestExtended);
}
catch (WebException we)
{
HttpWebResponse response = (HttpWebResponse)we.Response;
response.Close();
}
}
static void SaveResponse(IAsyncResult result)
{
var requestExtended = result.AsyncState as RequestExtended;
try
{
WebResponse response = requestExtended.Request.EndGetResponse(result);
if (requestExtended.CallBack != null) requestExtended.CallBack();
}
catch (WebException we)
{
HttpWebResponse response = (HttpWebResponse)we.Response;
response.Close();
}
}
I'm thinking about doing the same thing, but I've not come across anything yet.
I'm thinking about using JSON.net for the comms, so basically writing a REST service in GAE for the client to call, and maybe OAuth.NET for the authentication (unless I can find the .NET port of the google one, I've not looked yet)
Silverlight is basically just .NET, tho a lite version of it, so if you can find .NET code to do something, it should work, atleast somewhat, in SL :)
But thats as far as I've got - thinking about it. Sorry, can't be of more help yet!
I'm looking at this also. There are several REST projects for GAE, I haven't tried any of them out yet, but hope to in the next week or so.
http://code.google.com/p/app3/
http://code.google.com/p/gae-json-rest/
http://code.google.com/p/appengine-rest-server/
I couldn't find any examples getting Silverlight to work with google app's Java SDK, so here is my post.
Download the demo for Expression Blend. Check the included tutorial which shows how to create a gorgeous Silverlight interface in GUI mode and integrate it with the Bing search web service. Manipulating this example into a Google example should be trivial. Good luck! :)