Skip to content

Instantly share code, notes, and snippets.

@User9000
Forked from jonhurlock/pleaseEscape.py
Created April 9, 2019 01:58
Show Gist options
  • Save User9000/a823ba471c4a45a6d7a18b10b3e8b683 to your computer and use it in GitHub Desktop.
Save User9000/a823ba471c4a45a6d7a18b10b3e8b683 to your computer and use it in GitHub Desktop.
Example Python Code showing cURLing data and POSTing data to Elastic Search, but fails with escaped speech marks
############# My Clusters Health
curl -XGET 'http://127.0.0.1:9200/_cluster/health?pretty=true'
{
"cluster_name" : "TweetHadoop",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 15,
"active_shards" : 15,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 15
}
############# Works fine for posting data
import urllib
import urllib2
url = 'http://localhost:9200/twitter/tweet/22499999'
data = '{"user" : "helenax33","message" : "LIVE: http://www.justin.tv/xxhelenaxx i look like a can read+sound like a man.","pubDate" : "20090705T21:46:34","isAQuestion" : "0" }'
#data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
##### Also this works Fine for posting data.
import pycurl
apiURL = 'http://localhost:9200/twitter/tweet/22499999'
c = pycurl.Curl()
c.setopt(c.URL, apiURL)
c.setopt(c.POSTFIELDS, '{ "user" : "helenax33", "message" : "LIVE: http://www.justin.tv/xxhelenaxx i look like a can read+sound like a man.", "pubDate" : "20090705T21:46:34", "isAQuestion" : "0" }')
c.setopt(c.VERBOSE, True)
c.perform()
# However, if I want to put a speech mark (") in the message part, then it fails e.g.
import pycurl
apiURL = 'http://localhost:9200/twitter/tweet/22499999'
c = pycurl.Curl()
c.setopt(c.URL, apiURL)
c.setopt(c.POSTFIELDS, '{ "user" : "helenax33", "message" : "LIVE: http://www.justin.tv/xxhelenaxx i " look like a can read+sound like a man.", "pubDate" : "20090705T21:46:34", "isAQuestion" : "0" }')
c.setopt(c.VERBOSE, True)
c.perform()
# So I tried to escape the speech mark e.g.
import pycurl
apiURL = 'http://localhost:9200/twitter/tweet/22499999'
c = pycurl.Curl()
c.setopt(c.URL, apiURL)
c.setopt(c.POSTFIELDS, '{ "user" : "helenax33", "message" : "LIVE: http://www.justin.tv/xxhelenaxx i \" look like a can read+sound like a man.", "pubDate" : "20090705T21:46:34", "isAQuestion" : "0" }')
c.setopt(c.VERBOSE, True)
c.perform()
# However, this still fails. Please help :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment