#StackBounty: #python #python-3.7 #boost-python user-config.jam not being interpreted correctly on windows 10

Bounty: 50

I am trying to build the python bindings of libtorrent using boost version 1.63. I’ve set up my user-config.jam file in my home directory like so:

using python : 3.7 : C:/Program Files (x86)/Python/Python37-32/python.exe : C:/Program Files (x86)/Python/Python37-32/Include : C:/Program Files (x86)/Python/Python37-32/libs ;

However when I am running

bjam libtorrent-link=static boost-link=static stage_module

inside the directory of the python bindings of libtorrent I get the following error:

*** argument error
* rule python.init ( version ? : cmd-or-prefix ? : includes * : libraries ? : condition * : extension-suffix ? )
* called with: ( 3.7 : C:/Program Files (x86)/Python/Python37-32/python.exe : C:/Program Files (x86)/Python/Python37-32/Include : C:/Program Files (x86)/Python/Python37-32/libs :  :  :  :  )
* extra argument Files

I’ve done this before on Windows 7 but I haven’t experienced anything like that. I’ve tried all possible ways of formatting it like escaping spaces and parenthesis with a caret ^, using quotes ' or double quotes ". I’ve also tried using both forward slash and backslash. Everything yields the same error except for the double quotes:

'C:/Program' is not recognized as an internal or external command,
operable program or batch file.

Can anyone point out what I am doing wrong?


Get this bounty!!!

#StackBounty: #python #python-3.x #decorator Pass a decorator's function into Python RQ

Bounty: 50

How do I pass a decorator’s function into a job?

I have a decorator that would run a job using the function.

@job
def queueFunction(passedFunction, *args, **kwargs):
    # Do some stuff
    passedFunction(*args, **kwargs)

def myDecorator(async=True):
    def wrapper(function):
        def wrappedFunc(*args, **kwargs):
            data = DEFAULT_DATA
            if async:
                queueFunction.delay(function, *args, **kwargs)
            else:
                data = queueFunction(function, *args, **kwargs)
            return data
        return wrappedFunc
    return wrapper

I get an error when trying to use it.

Can't pickle <function Model.passedFunction at 0x7f410ad4a048>: it's not the same object as modelInstance.models.Model.passedFunction

Using Python 3.4


Get this bounty!!!

#StackBounty: #python #html #shell #download #wget Shell script to download a lot of HTML files and store them statically with all CSS

Bounty: 50

I have posted on a forum of sciences a lot of post (roughly 290 questions) that I would like to get back by downloading them with all the associated answers.

The first issue is that I have to be logged on my personal space to have the list of all the messages. How to circumvent this first barrier to be able with a shell script or a single wget command to get back all URL and their content. Can I pass to wgeta login and a password to be logged and redirected to the appropriate URL ontaining the list of all messages ?

Once this first issue will be solved, the second issue is that I have to start from 6 different menu pages that all contain the title and the link of the questions.

Moreover, concerning some of my questions, the answers and the discussions may be on multiple pages.

So I wonder if I could achieve this operation of global downloading knowing I would like to store them statically with local CSS stored also on my computer (to keep the same format into my browser when I consult them on my PC).

The URL of the first menu page of questions is (once I am logged on the website : that could be an issue also to download with wget if I am obliged to be connected).

An example of URL containing the list of messages, once I am logged, is :

https://forums.futura-sciences.com/search.php?searchid=22897684

the other pages (there all 6 or 7 pages of discussions title in total appering in the main menu page) have the format :
https://forums.futura-sciences.com/search.php?searchid=22897684&pp=&page=2
(for page 2)

https://forums.futura-sciences.com/search.php?searchid=22897684&pp=&page=5
(for page 5)

One can see on each of these pages the title and the link of each of the discussions that I would like to download with also CSS (knowing each discussion may contain multiple pages also) :

for example the first page of discussion “https://forums.futura-sciences.com/archives/804364-demonstration-dilatation-temps.html

has page2 : “https://forums.futura-sciences.com/archives/804364-demonstration-dilatation-temps-2.html

and page 3 : “https://forums.futura-sciences.com/archives/804364-demonstration-dilatation-temps-3.html

Naively, I tried to do all this with only one command : (with the example of URL on my personal space that I have taken at the beginning of post, i.e “https://forums.futura-sciences.com/search.php?searchid=22897684“) :

wget -r --no-check-certificate --html-extension --convert-links "https://forums.futura-sciences.com/search.php?searchid=22897684"

but unfortunately, this command downloads all files, and even maybe not what I want, i.e my discussions.

I don’t know what the approach to use : must I firstly store all URL in a file (with all sub-pages containing all answers and the global discussion for each of mu initial question) ?

and after, I could do maybe a wget -i all_URL_questions.txt

Anyone could give me help to carry out this operation?

UPDATE 1 : my issue needs a script, i tried with python the following things :

1)

import urllib, urllib2, cookielib

username = 'USERNAME'
password = 'PASSWORD'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'password' : password})
opener.open('https://forums.futura-sciences.com/login.php', login_data)
resp = opener.open('https://forums.futura-sciences.com/search.php?do=finduser&userid=253205&contenttype=vBForum_Post&showposts=1')
print resp.read()

But the page printed is not the page of my home into personal space.

2)

import requests

# Fill in your details here to be posted to the login form.
payload = { 
    'inUserName': 'USERNAME',
    'inUserPass': 'PASSWORD'
}

# Use 'with' to ensure the session context is closed after use.
with requests.Session() as s:
    p = s.post('https://forums.futura-sciences.com/login.php?do=login', data=payload)
    # print the html returned or something more intelligent to see if it's a successful login page.
    print p.text.encode('utf8')

    # An authorised request.
    r = s.get('https://forums.futura-sciences.com/search.php?do=finduser&userid=253205&contenttype=vBForum_Post&showposts=1')
    print r.text.encode('utf8')

here too, this doesn’t work

3)

import requests
import bs4 

site_url = 'https://forums.futura-sciences.com/login.php?do=login'
userid = 'USERNAME'
password = 'PASSWWORD'

file_url = 'https://forums.futura-sciences.com/search.php?do=finduser&userid=253205&contenttype=vBForum_Post&showposts=1' 
o_file = 'abc.html'  

# create session
s = requests.Session()
# GET request. This will generate cookie for you
s.get(site_url)
# login to site.
s.post(site_url, data={'vb_login_username': userid, 'vb_login_password': password})
# Next thing will be to visit URL for file you would like to download.
r = s.get(file_url)

# Download file
with open(o_file, 'wb') as output:
    output.write(r.content)
print("requests:: File {o_file} downloaded successfully!")

# Close session once all work done
s.close()

Same thing, the content is wrong

4)

from selenium import webdriver

# To prevent download dialog
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', '/tmp')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')

webdriver.get('https://forums.futura-sciences.com/')
webdriver.find_element_by_id('ID').send_keys('USERNAME')
webdriver.find_element_by_id ('ID').send_keys('PASSWORD')
webdriver.find_element_by_id('submit').click()
browser = webdriver.Firefox()
browser.get('https://forums.futura-sciences.com/search.php?do=finduser&userid=253205&contenttype=vBForum_Post&showposts=1')

Still not getting to log in with USERNAME and PASSSWORD and get content of homepage of personal space

5)

from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxProfile
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time

def MS_login(username, passwd):  # call this with username and password

firefox_capabilities = DesiredCapabilities.FIREFOX
    firefox_capabilities['moz:webdriverClick'] = False
    driver = webdriver.Firefox(capabilities=firefox_capabilities)
    fp = webdriver.FirefoxProfile()
    fp.set_preference("browser.download.folderList", 2) # 0 means to download to the desktop, 1 means to download to the default "Downloads" directory, 2 means to use the directory
    fp.set_preference("browser.download.dir","/Users/user/work_archives_futura/")
    driver.get('https://forums.futura-sciences.com/') # change the url to your website
    time.sleep(5) # wait for redirection and rendering
    driver.delete_all_cookies() # clean up the prior login sessions
    driver.find_element_by_xpath("//input[@name='vb_login_username']").send_keys(username)

elem  = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='vb_login_password']")))
    elem.send_keys(Keys.TAB)

driver.find_element_by_xpath("//input[@type='submit']").click()

    print("success !!!!")

driver.close() # close the browser
    return driver

if __name__ == '__main__':
    MS_login("USERNAME","PASSWORD")

The window is well opened, username filled but impossible to fill or submit the password and click on submit.

I begin to be discouraged.

ps: the main issue could come from that password field has display:none property, So I can’t simulate TAB operation to password field and pass it, once I have put the login.

Any help is welcome

EDIT 1: Isn’t there really nobody who could provide suggestions ? I know this a little bit tricky but it should exist a solution, at least, I hope…


Get this bounty!!!

#StackBounty: #python #data-visualization #r Is there an R or Python function to plot multivariate Gaussian mixtures?

Bounty: 50

Is there a package in R or a library in Python with some functions that allow a graphical representation of a multivariate (2 – dimensional) Gaussian mixture as a joint distribution? By this I mean that I’m not interested in the marginal distributions.

I’m not doing any sort of estimation or approximation, I would just like to visualize the pdf.


Get this bounty!!!

#StackBounty: #c# #python #.net #ironpython #python.net Running Python Code in .NET Environment without Installing Python

Bounty: 50

Is it possible to productionize Python code in a .NET/C# environment without installing Python and without converting the Python code to C#, i.e. just deploy the code as is?

I know installing the Python language would be the reasonable thing to do but my hesitation is that I just don’t want to introduce a new language to my production environment and deal with its testing and maintenance complications, since I don’t have enough manpower who know Python to take care of these issues.

I know IronPython is built on CLR, but don’t know how exactly it can be hosted and maintained inside .NET. Does it enable one to treat PYthon code as a “package” that can be imported into C# code, without actually installing Python as a standalone language? How can IronPython make my life easier in this situation? Can python.net give me more leverage?


Get this bounty!!!

#StackBounty: #python #dictionary update nested dictionary values from another nested dictionary based on mapping provided in dictionary

Bounty: 50

I need to write down a method to transform existing nested dictionary values from another nested dictionary , and mapping in between these two dictionary is in another third dictionary.

input_json = { 
              "s0" : 
              {
                 "s1"  :  {
                       "s1_f1":"s1_v1",
                       "s1_f2" : "s1_v2"
                     },
                  "s2":   {
                       "s2_f1":"s2_v1",
                       "s2_f2" : "s2_v2",
                       "s2_f3" : {
                                    "s3_f1":"s3_v1"
                                 }
                     }
              }
}

swagger_template = { "d0" : 
                     {
                       "d1_f1":"d1_v1",
                       "d3_f1" :"d3_v1"
                     }
            }

mapping = {
            "d1_f1":"s1_f1",
            "d3_f1" : "s3_f1"
          }

def find_mapping_key(v):  
      return (mapping[v])


def find_input_value(k,input_json):  
  if k in input_json:
        return input_json[k]
  for v in input_json.values():
        if isinstance(v, dict):
            return find_input_value(k,v)
  return None



def transform(swagger_template):  
  for k, v in swagger_template.items():   
    if isinstance(v, dict):
      transform(v)      
    else:
         print("{0} : {1}".format(k, v))
         #find the mapping value for this key in mapping JSON 
         print(find_mapping_key(k))
         mapping_key=find_mapping_key(k)
         #find the value for that mapping key in input_json 
         print(find_input_value(mapping_key,input_json))




transform(swagger_template)

its giving correct value for one mapping but for another one its returned NONE .

enter image description here


Get this bounty!!!

#StackBounty: #python #python-requests How to login using requests in Python?

Bounty: 50

I would like to get data from https://creis.fang.com/.

However, I need to login the page first.

There are 4 values I need to fill in.

I tried to use requests but failed.

Here is my code:

import requests
url = 'https://creis.fang.com/'
s = requests.Session()
data ={'cnname': 'myname', 'cnotp':'abc', 'cntempcode':'123', 'cnproducttitle':'企业版'}
r = s.post(url=url, data=data)

Can you help me?

Thanks.

enter image description here

enter image description here
enter image description here
enter image description here


Get this bounty!!!

#StackBounty: #python #ssl #certificate Determine if SSL certificate is self signed using Python

Bounty: 200

I am trying to determine if an SSL certificate is self signed or not. Currently I have the following code which compares the issuer CN and the subject CN and if they are the same, marks the result as self signed.

with open(cert_file, "r") as f: 
    x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, f.read())

result = {
    'subject': dict(x509.get_subject().get_components()),
    'issuer': dict(x509.get_issuer().get_components()),
    'serialNumber': x509.get_serial_number(),
    'version': x509.get_version(),
    'notBefore': datetime.strptime(x509.get_notBefore(), '%Y%m%d%H%M%SZ'),
    'notAfter': datetime.strptime(x509.get_notAfter(), '%Y%m%d%H%M%SZ'),
}

extensions = (x509.get_extension(i) for i in range(x509.get_extension_count()))
extension_data = {e.get_short_name(): str(e) for e in extensions}
result.update(extension_data)

if result['issuer']['CN'] == result['subject']['CN']:
    result.update({'self-signed': True})
else:
    result.update({'self-signed': False})

This comparison is very simplistic, but works in a lot of cases. I’m not trying to verify SSL certs or reimplement OpenSSL. How can I make this better and be roughly 95% sure if a certificate is self signed or not?

My one requirement is that I would like to do this in Python and not call other processes or use shell commands.


Get this bounty!!!

#StackBounty: #python #django ProfileForm that collects extra fields on User Signup

Bounty: 50

I have a signupView that shows 2 forms: SignUpForm and ProfileForm.

Basically, SignUpForm collects data like first_name, last_name, user_name, email, password1, password2.

And ProfileForm collects data like dni, birthdate, shipping_address, etc.

I’m using this approach because is what I know. But was wondering if I can collect the fields in ProfileForm in just one form, the SignUpForm.

views.py

@transaction.atomic
def signupView(request):
    peru = Peru.objects.all()
    department_list = set()
    province_list = set()
    district_list = set()
    for p in peru:
        department_list.add(p.departamento)
    department_list = list(department_list)
    if len(department_list):
        province_list = set(Peru.objects.filter(departamento=department_list[0]).values_list("provincia", flat=True))
       province_list = list(province_list)
    else:
        province_list = set()
    if len(province_list):
        district_list = set(
        Peru.objects.filter(departamento=department_list[0], provincia=province_list[0]).values_list("distrito",
                                                                                                     flat=True))
    else:
        district_list = set()

    if request.method == 'POST':

        user_form = SignUpForm(request.POST)
        profile_form = ProfileForm(district_list, province_list, department_list, request.POST, request.FILES) 

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save(commit=False)
            user.is_active = False
            user.save()
            username = user_form.cleaned_data.get('username')
            signup_user = User.objects.get(username=username)
            customer_group = Group.objects.get(name='Clientes')
            customer_group.user_set.add(signup_user)
            raw_password = user_form.cleaned_data.get('password1')
            user.refresh_from_db()  # This will load the Profile created by the Signal

            profile_form = ProfileForm(district_list, province_list, department_list, request.POST, request.FILES,
                                       instance=user.profile)  # Reload the profile form with the profile instance
            profile_form.full_clean()  # Manually clean the form this time. It is implicitly called by "is_valid()" method
            profile_form.save()  # Gracefully save the form

            return redirect('shop:email_confirmation_needed')

    else:

        user_form = SignUpForm()

        profile_form = ProfileForm(district_list, province_list, department_list)

    return render(request, 'accounts/signup.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })

models.py

### User Profile ###

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    birthdate = models.DateField(null=True, blank=True)
    dni = models.CharField(max_length=30, blank=True)
    phone_number = models.CharField(max_length=30, blank=True)
    shipping_address1 = models.CharField(max_length=100, blank=False)
    reference = models.CharField(max_length=100, blank=False)
    shipping_department = models.CharField(max_length=100, blank=False)
    shipping_province = models.CharField(max_length=100, blank=False)
    shipping_district = models.CharField(max_length=100, blank=False)
    photo = models.ImageField(upload_to='profile_pics', default='profile_pics/default_profile_pic_white.png')

    def __str__(self):
        return str(self.user.first_name) + "'s profile"




@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
    instance.profile.save()

forms.py:

class SignUpForm(UserCreationForm):
    error_messages = {
        'password_mismatch': "Las contraseñas no coinciden.",
    }

    first_name = forms.CharField(label="Nombre", max_length=100, required=True)
    last_name = forms.CharField(label='Apellido', max_length=100, required=True)
    username = forms.CharField(label='Nombre de usuario', max_length=100, required=True,
                               error_messages={'invalid': "you custom error message"})
    email = forms.EmailField(label='Correo electrónico', max_length=60, required=True)
    password1 = forms.CharField(label='Contraseña', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Confirmar contraseña', widget=forms.PasswordInput)

    def __init__(self, *args, **kwargs):
        super(SignUpForm, self).__init__(*args, **kwargs)

        for fieldname in ['username', 'password1', 'password2']:
            self.fields[fieldname].help_text = None

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError(
                self.error_messages['password_mismatch'],
                code='password_mismatch',
            )
        return password2


    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'username', 'email', 'password1',
                  'password2')


class ProfileForm(ModelForm):
    MONTHS = {
        1:'ene', 2:'feb', 3:'mar', 4:'abr',
        5:'may', 6:'jun', 7:'jul', 8:'ago',
        9:'set', 10:'oct', 11:'nov', 12:'dic'
    }

    def __init__(self, district_list, province_list, department_list, *args, **kwargs):
        super(ProfileForm, self).__init__(*args, **kwargs)
        self.fields['shipping_district'] = forms.ChoiceField(label='Distrito', choices=tuple([(name, name) for name in district_list]))
        self.fields['shipping_province'] = forms.ChoiceField(label='Provincia', choices=tuple([(name, name) for name in province_list]))
        self.fields['shipping_department'] = forms.ChoiceField(label='Departamento', choices=tuple([(name, name) for name in department_list]))


    dni = forms.CharField(label='DNI', max_length=100, required=True)
    phone_number = forms.CharField(label='Celular')
    birthdate = forms.DateField(label='Fecha de nacimiento', widget=SelectDateWidget(years=range(1980, 2012), months=MONTHS))
    shipping_address1 = forms.CharField(label='Dirección de envío', max_length=100, required=True)
    shipping_address2 = forms.CharField(label='Referencia (opcional)', max_length=100, required=False)

    class Meta:
        model = Profile
        fields = ('dni', 'phone_number', 'birthdate', 'shipping_address1',
                  'shipping_address2', 'shipping_department', 'shipping_province', 'shipping_district')


Get this bounty!!!

#StackBounty: #php #python #python-3.x #flask #hmac Hmac verification with flask in Python (with reference in PHP and RUBY)

Bounty: 50

Hey guys I’ve been working on a way to implement HMAC verification in python with flask for the selly.gg merchant website.

So selly’s dev documentation give these following examples to verify HMAC signatures (in PHP and ruby): https://developer.selly.gg/?php#signing-validating
(code below:)

PHP:

<?php
        $signature = hash_hmac('sha512', json_encode($_POST), $secret);
        if hash_equals($signature, $signatureFromHeader) {
            // Webhook is valid 
        }
?>

RUBY:

signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha512'), secret, payload.to_json)
is_valid_signature = ActiveSupport::SecurityUtils.secure_compare(request.headers['X-Selly-Signature'], signature)

So, so far what I could figure out: They don’t encode with base64 (like shopify and others do), it uses SHA-512, it encodes the secret code alongside json response data and finally the request header is ‘X-Selly-Signature’

I’ve made the following code so far (based on shopify’s code for HMAC signing https://help.shopify.com/en/api/getting-started/webhooks):

SECRET = "secretkeyhere"
def verify_webhook(data, hmac_header):
    digest = hmac.new(bytes(SECRET, 'ascii'), bytes(json.dumps(data), 'utf8'), hashlib.sha512).hexdigest()
    return hmac.compare_digest(digest, hmac_header)
try:
    responsebody = request.json #line:22
    status = responsebody['status']#line:25
except Exception as e:
    print(e)
    return not_found()
print("X Selly sign: " + request.headers.get('X-Selly-Signature'))
verified = verify_webhook(responsebody, request.headers.get('X-Selly-Signature'))
print(verified)

However selly has a webhook simulator, and even with the proper secret key and valid requests, the verify_webhook will always return False. I tried contacting Selly support, but they couldn’t help me more than that

You can test the webhook simulator at the following address:
https://selly.io/dashboard/{your account}/developer/webhook/simulate


Get this bounty!!!