Python HTTP request bad character encoding - python

I am uploading JSON data content to an elasticsearch index using python http.client. I successfully achieve to put the data but I'm having a char issue. Once inserted, special chars like é are outputed like é.
Here is the code :
import http.client
connection = http.client.HTTPConnection(elastic_address)
headers = {"Content-type": "application/json", "Accept": "text/plain"}
connection.request('PUT', url=endpoint, headers = headers, body=json_data.encode('utf-8'))
I have noticed that if I change the special chars in the source JSON before sending it like é replaced by \u00E9, it's working fine. It may be because Elasticsearch uses another char encoding but according to this link, ES uses utf-8 as character coding.
I've also overviewed the client.py of the http.client package and it seems that the data are encoded in latin-1, see below :
def _encode(data, name='data'):
"""Call data.encode("latin-1") but show a better error message."""
try:
return data.encode("latin-1")
except UnicodeEncodeError as err:
raise UnicodeEncodeError(
err.encoding,
err.object,
err.start,
err.end,
"%s (%.20r) is not valid Latin-1. Use %s.encode('utf-8') "
"if you want to send it encoded in UTF-8." %
(name.title(), data[err.start:err.end], name)) from None
I'm not sure where the issue is, in the script? in the http.client package? in the Elasticsearch index settings?
Any idea?

Related

How to send POST request with each payload on its own line using Python requests

I have to send a POST request to the /batch endpoint of : 'https://www.google-analytics.com'.
As mentioned in the Documentation I have to send the request to /batch endpoint and specify each payload on its own line.
I was able to achieve this using POSTMAN as follows:
My query is to make a POST request using Python's requests library
I tried something like this :
import requests
text = '''v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=bookmarks&ev=13
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=upvotes&ev=65
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=questions&ev=15
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=postviews&ev=95'''
response = requests.post('https://www.google-analytics.com/batch', data=text)
but it doesn't works.
UPDATE
I Tried this and it works !
import http.client
conn = http.client.HTTPSConnection("www.google-analytics.com")
payload = "v=1&cid=43223523&tid=UA-200248207-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=bookmarks&ev=13\r\nv=1&cid=43223523&tid=UA-200248207-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=upvotes&ev=63\r\nv=1&cid=43223523&tid=UA-200248207-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=questions&ev=11\r\nv=1&cid=43223523&tid=UA-200248207-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=postviews&ev=23"
headers = {
'Content-Type': 'text/plain'
}
conn.request("POST", "/batch", payload, headers)
res = conn.getresponse()
But the question remains open, what's the issue with requests here.
You don't need to double-escape the newline symbol.
Moreover, you don't need the newline symbol at all for the multi-line string.
And also the indentations you put in your multi-line string are counted:
test = '''abc
def
ghi'''
print(test)
Here's an SO answer that explains this with some additional ways to make long stings: https://stackoverflow.com/a/10660443/4570170
Now the request body.
The documentation says
payload_data – The BODY of the post request. The body must include exactly 1 URI encoded payload and must be no longer than 8192 bytes.
So try uri-encoding your payload:
text = '''v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=bookmarks&ev=13
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=upvotes&ev=65
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=questions&ev=15
v=1&cid=43223523&tid=UA-XXXXXX-1&t=event&ec=aggregated_stats&ea=daily_kpi&el=postviews&ev=95'''
text_final = requests.utils.quote(text)
response = requests.post('https://www.google-analytics.com/batch', data=text_final)
Finally , I figured out the solution myself.
Updating for others help.
The problem was I was working on AWS Cloud9 and as mentioned in the documentation
Some environments are not able to send hits to Google Analytics directly. Examples of this are older mobile phones that can't run JavaScript or corporate intranets behind a firewall.
So we just need to include the User Agent parameter
ua=Opera/9.80
in each of our payloads
It works !

What kind of base64 is used in HTTP responses?

I'm catching HTTP requests made by Selenium using Browser Mob Proxy (using the browsermob-proxy Python package). In my HAR file, I see this (it should be a Javascript file):
"content": {
"comment": "",
"size": 10908,
"mimeType": "application/x-javascript; charset=utf-8",
"encoding": "base64",
"text": "4qF2FJIUvnlRlDNSpoB6KeBkiFADdf4D8QYKthNRVc+tMqHv1vuipJbkMKsgPuuM4/FG3RVcLlYhxHmH35SDP/NTPTs+PWuP0OSUmpep2uur7Gg0QAyJLhUO46Fajm7qKW2HIkAKFgUwAKh2pplqVvWqnKqQtoMInI5jpiEhh7QO8b+zQ+HSllatf4Oq8hdgAzWVjJFBy4K7J02g0CWJaE3ikUbWh96mImD0tVD/hsgTm03zkZSKPTbyuux6ZjVB07R8fS9NIHHcSSsOkdejy1htoCuWa6qNMM7z2VERAsm2YnW3oRpFsJkUvrT3/99X7Z1ffUj6En9wyp2bLmQNl4tmOqemFs7Z517zBcDzAJBLIEGuERUmkNLE8M4NjyCCDEDgMIy0VvwpaBxCrmLponHp1adYxq6kOjeldTMD6cmsD03id+swTblb2+2X1oTXgDEGDCHbH9Pv+feptbckYYUdiFQCJAkgFKl3f8tnz3Np6clo7Ew75vzG+LaQOB1e21ynJe35eaQA+I2sEYlCgm6h+3cbWLVx4P2jO4754x/xT0lPemYveuX+jAjSCQeYXsCHqHK+xS9AIgSCvgDnhNzk3IcLxJEmwqIAJz+ccdWDC0AiQxA8QvMlzrniwQWgl6P//xfAEdO2ruZucLVYmUp6L8E5KE8gPqpjGVdkWCnJMJDqyq3bEkxdVBdt/dWpFB/n7JTjKAWquRsjYY49sj6MU0mu8+tOomC5okbz1zk+mvUs80FeweRRKRYW4FyjaRrQBbAOCQRMnIgzT/64QJrkePBXCl7JmhzbXBUevKs1h14bjs1sUmtKqH+rLB0fQUqwhDTrVBagWlKH5wlnIY2dCSIkOfKSIVexLI+XuLxVOn/IghTLTaLnbwpql4VSlGrJbyLmJMdECxS2wSLYQVOELiAoMBGGbBd9sD0GepfYYnCfAeElLGvqTq0ShuoNNuhNfKW/rO/0l/Wl/rC+xRoqZxy6pA2MmmIat8dp0tUbzfRBSHijlOJ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6Xh1BXXMtwqkVwMKchBGNDKJZWhQB+CSVqkiMa3nxZ3WNKFddMiSk5enWGBI3hnEfxIYU7j3JkGCEaemaFPh9rV895sJhdxfTL1XHrWSkCMVRkd6VU5A/dCo2qSIm6g/UeH1aB2OyCjMNmPlMPn0vZuxI+pWJ/bJMKYZadfX8QdEuD8SSj0g4AnW1QYjPHfWaiAKxkbhJVmdxMiG0QU1DWz4DeZptmkHDQhRW5idMqzifRbpkqGdOvx08wiE6Kz6dSvkK7x5p4zrH9GeE8eLFuRbwq28v2kiT6jskPS7okJj4VkziTQSommnr0xScVQ5bN2R3/ZxU99rnZOfPbZByxGCTfmSXk7aRQ5I8d0ncXbzEsW0Lkk4tLVWuUNsgatviVbiTbhOL6LKq0RN7no5kNlUjbLwubt584R4nF/ZRFC+DnORNTSD75rukAMq35s3RPSlvVeK703y3z5d8erCRa6543oNbPg6qtyN8O7LOPvFAUR0AhCyTP4q+A64qO4R1ludQXWImoSjdNMwgsiZHgHQG"
}
However, when decoding the text of the content using
base64.b64decode(my_coded_text).decode("UTF-8")
I invariably receive the following error:
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 0-1: invalid continuation byte
What's the proper way to decode such strings?
EDITED TO ADD
I'm assuming the issue comes from this line from rfc1341: "A CRLF sequence in base64 data should be converted to a quoted-printable line break, but ONLY when converting text data" because decoding works fine with images.
However, I've yet to understand what's a CRLF sequence in base64 data and how it can be converted.
I'm not sure what kind of data is supposed to be in the "text" property, but it sure isn't text. You'll need to analyze the application that made this request to figure this out.
The decoded text starts out with the following data, which doesn't contain any readable text, and doesn't match with any known file format:
00000000: e2a1 7614 9214 be79 5194 3352 a680 7a29 ..v....yQ.3R..z)
00000010: e064 8850 0375 fe03 f106 0ab6 1351 55cf .d.P.u.......QU.
00000020: ad32 a1ef d6fb a2a4 96e4 30ab 203e eb8c .2........0. >..
00000030: e3f1 46dd 155c 2e56 21c4 7987 df94 833f ..F..\.V!.y....?
An analysis of the data shows that it's virtually incompressible. This means that it may be encrypted, or compressed with an unknown algorithm.
I couldn't find a way to decode the base64 but I could find a workaround: sending the same request using selenium-requests and accessing the response which is, this time, not base64-encoded.
Code now looks like this:
for http_req in proxy.har["log"]["entries"]:
req_url = http_req["request"]["url"]
if url in req_url:
# Captures the POST data
payload = {}
for post_param in http_req["request"]["postData"]["params"]:
param_name = post_param["name"]
param_value = post_param["value"]
payload[param_name] = param_value
# Resends the post request
r = driver.request('POST', url, data=payload)

"ConnectionResetError" What should I do?

# -*- coding: UTF-8 -*-
import urllib.request
import re
import os
os.system("cls")
url=input("Url Link : ")
if(url[0:8]=="https://"):
url=url[:4]+url[5:]
if(url[0:7]!="http://"):
url="http://"+url
try :
try :
value=urllib.request.urlopen(url,timeout=60).read().decode('cp949')
except UnicodeDecodeError :
value=urllib.request.urlopen(url,timeout=60).read().decode('UTF8')
par='<title>(.+?)</title>'
result=re.findall(par,value)
print(result)
except ConnectionResetError as e:
print(e)
TimeoutError is disappeared. But ConnectionResetError appear. What is this Error? Is it server problem? So it can't solve with me?
포기하지 마세요! Don't give up!
Some website require specific HTTP Header, in this case, User-agent. So you need to set this header in your request.
Change your request like this (17 - 20 line of your code)
# Make request object
request = urllib.request.Request(url, headers={"User-agent": "Python urllib test"})
# Open url using request object
response = urllib.request.urlopen(request, timeout=60)
# read response
data = response.read()
# decode your value
try:
value = data.decode('CP949')
except UnicodeDecodeError:
value = data.decode('UTF-8')
You can change "Python urllib test" to anything you want. Almost every servers use User-agent for statistical purposes.
Last, consider using appropritate whitespaces, blank lines, comments to make your code more readable. It will be good for you.
More reading:
HTTP/1.1: Header Field Definitions - to understand what is User-agent header.
21.6. urllib.request — Extensible library for opening URLs — Python 3.4.3 documentation - Always read documentation. Link to urllib.request.Request section.

making a simple GET/POST with url Encoding python

i have a custom url of the form
http://somekey:somemorekey#host.com/getthisfile.json
i tried all the way but getting errors :
method 1 :
from httplib2 import Http
ipdb> from urllib import urlencode
h=Http()
ipdb> resp, content = h.request("3b8138fedf8:1d697a75c7e50#abc.myshopify.com/admin/shop.json")
error :
No help on =Http()
Got this method from here
method 2 :
import urllib
urllib.urlopen(url).read()
Error :
*** IOError: [Errno url error] unknown url type: '3b8108519e5378'
I guess something wrong with the encoding ..
i tried ...
ipdb> url.encode('idna')
*** UnicodeError: label empty or too long
Is there any way to make this Complex url get call easy .
You are using a PDB-based debugger instead of a interactive Python prompt. h is a command in PDB. Use ! to prevent PDB from trying to interpret the line as a command:
!h = Http()
urllib requires that you pass it a fully qualified URL; your URL is lacking a scheme:
urllib.urlopen('http://' + url).read()
Your URL does not appear to use any international characters in the domain name, so you do not need to use IDNA encoding.
You may want to look into the 3rd-party requests library; it makes interacting with HTTP servers that much easier and straightforward:
import requests
r = requests.get('http://abc.myshopify.com/admin/shop.json', auth=("3b8138fedf8", "1d697a75c7e50"))
data = r.json() # interpret the response as JSON data.
The current de facto HTTP library for Python is Requests.
import requests
response = requests.get(
"http://abc.myshopify.com/admin/shop.json",
auth=("3b8138fedf8", "1d697a75c7e50")
)
response.raise_for_status() # Raise an exception if HTTP error occurs
print response.content # Do something with the content.

Decode Cookie variable extracted from a HTTP Stream - Python

I am using python to send a request to a server. I get a cookie from the server. I am trying to decode the encoding scheme used by the server - I suspect it's either utf-8 or base64.
So I create my header and connection objects.
resp, content = httpobj.request(server, 'POST', headers=HTTPheader, body=HTTPbody)
And then i extract the cookie from the HTTP Stream
cookie= resp['set-cookie']
I have tried str.decode() and unicode() but I am unable to get the unpacked content of the cookie.
Assume the cookie is
MjAyMTNiZWE4ZmYxYTMwOVPJ7Jh0B%2BMUcE4si5oDcH7nKo4kAI8CMYgKqn6yXpgtXOSGs8J9gm20bgSlYMUJC5rmiQ1Ch5nUUlQEQNmrsy5LDgAuuidQaZJE5z%2BFqAJPnlJaAqG2Fvvk5ishG%2FsH%2FA%3D%3D
The output I am expecting is
20213bea8ff1a309SÉì˜tLQÁ8².hÁûœª8<Æ
*©úÉzµs’Ïö¶Ñ¸•ƒ$.kš$5gQIPf®Ì¹,8�ºèA¦IœöZ€$ùå% *ao¾Nb²¶ÁöÃ
Try like this:
import urllib
import base64
cookie_val = """MjAyMTNiZWE4ZmYxYTMwOVPJ7Jh0B%2BMUcE4si5oDcH7nKo4kAI8CMYgKqn6yXpgtXOSGs8J9gm20bgSlYMUJC5rmiQ1Ch5nUUlQEQNmrsy5LDgAuuidQaZJE5z%2BFqAJPnlJaAqG2Fvvk5ishG%2FsH%2FA%3D%3D"""
res = base64.b64decode(urllib.unquote(cookie_val))
print repr(res)
Output:
"20213bea8ff1a309S\xc9\xec\x98t\x07\xe3\x14pN,\x8b\x9a\x03p~\xe7*\x8e$\x00\x8f\x021\x88\n\xaa~\xb2^\x98-\\\xe4\x86\xb3\xc2}\x82m\xb4n\x04\xa5`\xc5\t\x0b\x9a\xe6\x89\rB\x87\x99\xd4RT\x04#\xd9\xab\xb3.K\x0e\x00.\xba'Pi\x92D\xe7?\x85\xa8\x02O\x9eRZ\x02\xa1\xb6\x16\xfb\xe4\xe6+!\x1b\xfb\x07\xfc"
Of course the result here is a 8-bit string, so you have to decode it to get the the string that you want, i'm not sure which encoding to use, but there is the decoding result using the unicode-escape (unicode literal) :
>>> print unicode(res, 'unicode-escape')
20213bea8ff1a309SÉìtãpN,p~ç*$1ª~²^-\ä³Â}m´n¥`ÅBÔRT#Ù«³.K.º'PiDç?¨ORZ¡¶ûäæ+!ûü
Well Hope this can help .

Categories