Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Python shuffle list

i need to randomly shuffle positioning of User-Agent,Cache-Control, Accept-Charset, Referer and keep alive but how? help python pro


def httpcall(url):
code = 0
try:
requests.get(
url,
params={buildblock(): buildblock()},
headers={
'User-Agent': choice(useragents),
'Cache-Control': choice(['no-cache', 'max-age=0']),
'Accept-Charset': 'ISO-8859-1,UTF-8;q=0.7,*;q=0.7',
'Referer': get_referer() + buildblock(),
'Keep-Alive': randint(110, 120)}
)
You can't shuffle a dictionary in python
What do you mean shuffling the User-Agent, Cache-Control, Accept-Charset, etc...

Changing the order in a dictionary does not make a difference. I'll give you an example

PHP Code

<?php 
firstDict
= {"User": "Ewil", "Pass": "FooBar"}
secondDict = {"Pass": "Foobar", "User": "Ewil"}
print(
firstDict == secondDict) #True 

The order of the key-value pairs in a dictionary won't make a difference to the dictionary itself.
Dictionaries are unrecorded containers in Python. This means that there is no way to change the ordering manually. There is a container in the collections module called OrderedDict, but that may not be a solution. You could, for example, do something like this

PHP Code

<?php 
from random import choice
, randint, shuffle
from collections import OrderedDict

data 
= {
    
'User-Agent': choice(["user-agent1", "user-agent2"]),
    
'Cache-Control': choice(['no-cache', 'max-age=0']),
    
'Accept-Charset': 'ISO-8859-1,UTF-8;q=0.7,*;q=0.7',
    
'Referer': "foobar",
    
'Keep-Alive': randint(110, 120)
}
print(
data)
l = list(data.items())
shuffle(l)
od = OrderedDict(l)
print(
od) 

This prints
PHP Code

<?php 
{'Referer': 'foobar', 'Keep-Alive': 118, 'Cache-Control': 'max-age=0', 'User-Agent': 'user-agent2', 'Accept-Charset': 'ISO-8859-1,UTF-8;q=0.7,*;q=0.7'}
OrderedDict([('Cache-Control', 'max-age=0'), ('Accept-Charset', 'ISO-8859-1,UTF-8;q=0.7,*;q=0.7'), ('User-Agent', 'user-agent2'), ('Referer', 'foobar'), ('Keep-Alive', 118)]) 

Which is good... However, requests may not iterate through the ordered dict correctly in a way that preserves this order. This is an active issue: https://github.com/kennethreitz/requests/issues/3038
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
(Mar 20, 2016 11:28 AM)Goodies Wrote:  
PHP Code

<?php 
from random import choice
, randint, shuffle
from collections import OrderedDict

data 
= {
    
'User-Agent': choice(["user-agent1", "user-agent2"]),
    
'Cache-Control': choice(['no-cache', 'max-age=0']),
    
'Accept-Charset': 'ISO-8859-1,UTF-8;q=0.7,*;q=0.7',
    
'Referer': "foobar",
    
'Keep-Alive': randint(110, 120)
}
print(
data)
l = list(data.items())
shuffle(l)
od = OrderedDict(l)
print(
od) 

Thumbs up to Goodies for coming up with a solution rather than my feeble statement saying it was not possible.

Nothing is impossible, the word itself says 'I'm possible'!
- Audrey Hepburn
(Mar 20, 2016 12:01 PM)EwilDave Wrote:  Thumbs up to Goodies for coming up with a solution rather than my feeble statement saying it was not possible.

Nothing is impossible, the word itself says 'I'm possible'!
- Audrey Hepburn

I'm honestly not sure if it is a solution :/ first if all, it's VERY slow to have to turn a dict to a list of tuples, then shuffling is pretty fast, but then turning it back into an OrderedDict O.o
For this situation, though, it's plenty fast. But secondly, I'm honestly not sure if it even works. I'll test it out when I get home and I'll report back.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by Katе UptonJul 5, 2019 11:51 AM 6 213 Jul 5, 2019 11:51 AM
Last Post: l0lwtf
[Python] by Ineed2learn2eatJul 13, 2017 10:48 AM 3 230 Jul 13, 2017 10:48 AM
Last Post: Ineed2learn2eat