TypeError: expected string or Unicode object, NoneType found - Multiprocessing Pool not working in Zope/Plone external methods - plone

I'm using
Zope - 2.13.19
Python - 2.6.8
The below piece of code works when run manually but not when in External method.
It throws the following error. Am I doing something conceptually wrong ?
Exception in thread Thread-3:
Traceback (most recent call last):
File "/opt/python2.6/lib/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "/opt/python2.6/lib/python2.6/threading.py", line 484, in run
self.__target(*self.__args, **self.__kwargs)
File "/opt/python2.6/lib/python2.6/multiprocessing/pool.py", line 225, in _handle_tasks
put(task)
TypeError: expected string or Unicode object, NoneType found
import time
from multiprocessing import Pool
import logging
def func(name):
print 'hello %s,' % name
time.sleep(5)
print 'nice to meet you.'
def get_data():
pool = Pool(processes=2)
pool.map(func, ('frank', 'justin', 'osi', 'thomas'))

Make sure all the things you're sending across process boundaries can be pickled.
As stated by Multimedia Mike:
It is possible to send objects across process boundaries to worker
processes as long as the objects can be pickled by Python's pickle
facility.

Related

flask installation error in running _init_.py

I have this error while installing Flask in command prompt:
(flask) C:\myproject\app>python_init_.py
Traceback (most recent call last):
File "_init_.py", line 3, in <module>
app = Flask<_name_>
NameError: name '_name_' is not defined
What does this mean, and how can I fix it?
The dunder name variable is __name__ with two underscores (for init.py by the way) on both side. Also your should have parenthesis not <>:
app = Flask(__name__)
You can read more about dunder variable and methods in the official documentation.
Sidenote:
A common convention in python is to name your main package by the name of your project in lowercase. From that I tend to use __package__ instead of __name__. Flask example are simple enough to work in a one file structure but in real world you probably gonna have more. Therefore __name__ will be __init__ from the module name while __package__ will be equal to project/app name

PermissionError: [Errno 1] Operation not permitted

I am completely new to python, linux RPI and scapy. I am trying to send some packets using scapy.
On Command Line (only if super user privileges are given to scapy)
send(IP(dst="1.2.3.4")/ICMP())
This works perfectly, while running on python script.
from scapy.all import *
p=send(IP(dst="1.2.3.4")/ICMP())
Throws an error
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
send(IP(dst="1.2.3.4")/ICMP())
File "/usr/local/lib/python3.4/dist-packages/scapy/sendrecv.py",line 255, in send
__gen_send(conf.L3socket(*args, **kargs), x, inter=inter, loop=loop, count=count,verbose=verbose, realtime=realtime)
File "/usr/local/lib/python3.4/dist-packages/scapy/arch/linux.py", line 326, in __init__
self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type))
File "/usr/lib/python3.4/socket.py", line 123, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
PermissionError: [Errno 1] Operation not permitted<br>
I am trying to solve it but cannot, I am new to the environment therefore don't know much. As far as my search goes this issue is relevant to sockets. But I still need some simple explanation to understand.
This means that you need to start your script with sudo/admin rights.

Why the imported PowerFactory module in python can only execute single time?

The script is be able to run a software called PoiwerFctory externally by Python as follows:
#add powerfactory.pyd path to python path
import sys
sys.path.append("C:\\Program Files\\DIgSILENT\\PowerFactory 2017
SP2\\Python\\3.6")
#import powerfactory module
import powerfactory
#start powerfactory module in unattended mode (engine mode)
app=powerfactory.GetApplication()
#get the user
user=app.GetCurrentUser()
#active project
project=app.ActivateProject('Python Test') #active project "Python Test"
prj=app.GetActiveProject #returns the actived project
#run python code below
ldf=app.GetFromStudyCase('ComLdf') #caling loadflow command object
ldf.Execute() #executing the load flow command
#get the list of lines contained in the project
Lines=app.GetCalcRelevantObjects('*.ElmLne') #returns all relevant objects,
i.e. all lines
for line in Lines: #get each element out of list
name=line.loc_name #get name of the line
value=line.GetAttribute('c:loading') # return the value of elements
#Print the results
print('Loading of the line: %s = %.2f'%(name,value))
When the above code first time executed in Spyder, it will show proper resutls. However, if re-executing the script again, the following error is appeared:
Reloaded modules: powerfactory
Traceback (most recent call last):
File "<ipython-input-9-ae989570f05f>", line 1, in <module>
runfile('C:/Users/zd1n14/Desktop/Python Test/Call Digsilent in
Python.py', wdir='C:/Users/zd1n14/Desktop/Python Test')
File "C:\ProgramData\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/zd1n14/Desktop/Python Test/Call Digsilent in Python.py",
line 12, in <module>
user=app.GetCurrentUser()
RuntimeError: 'powerfactory.Application' already deleted
Referred to How can I exit powerfactory using Python in Unattended mode?, this may because of PowerFactory in still running. And the only way which has been found so far is to re-start the Spyder and execute the script again, this is so inefficiency that if I want to re-write the code and debugging it.
It would be so much appropriated that if anyone could give me some advice for such problem.
I ran into the same Problem. Python is still connected to powerfactory and gives the Error if you try to connect again. What basicly worked for me was to kill the instance on the end of your skript with
del app
another idea during debugging could be:
try:
# Do something in your skript
finally:
del app
So the killing of the instance happens in any case.
The way to solve this is to reload the powerfacotry module by adding:
if __name__ == "__main__":
before import powerfacory.
The reason behind may referred to: What does if __name__ == "__main__": do?.

problems with monkeyrunner

I am working on making some changes to the android framework layer and building my own version. I am working based on froyo and trying to use monkeyrunner for some testing. I have pulled the source and can build and run in the emulator but when I try to use a monkeyrunner script I can't seem to get anyhting to work. I built the code using lunch full-eng and it runs fine on the device. I am just trying to get a simple script running based on the example at http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html shown below with a print statement added just to see if I could get anything to run.
/# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
/# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
print "Hello World!"
When the following line is in the script I get an error as follows.
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
Traceback (most recent call last):
File "../../MRTesting/MyTest.py, line 4, in
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
ImportError: cannot import name MonkeyDevice
So if I remove MonkeyDevice from the import as shown below I get a different error on the call to waitForConnection()
from com.android.monkeyrunner import MonkeyRunner
Traceback (most recent call last):
File "../../MRTesting/MyTest.py, line 6, in
device = MonkeyRunner.waitForConnection()
AttributeError: type object 'com.android.monkeyrunner.MonkeyRunner' has no attribute 'waitForConnection'
I tried modifying the call to have some arguments as indicated in the documentation as follows but I still get the same error. The second argument matches the value returned by a call to adb devices.
device = MonkeyRunner.waitForConnection(5, 'emulator-5554')
I have done some digging around and one person said that the shebang needs to be at the beginning of the file as follows with the path modified to avoid putting information in I would rather not share.
/#! /home/<path>/monkeyrunner
I could not see how this would be any different than me invoking monkeyrunner directly from the command line but I tried it and no luck. I did not install the sdk anywhere on my system as it is included in the build tree but it seems to me that the monkeyrunner tool might not be able to locate it as needed but I can't find any indication of how to fix this. I am running the following commands when I build my system from within my build directory at the root.
. build/envsetup.sh
setpaths
lunch full-eng
make -j16
Anyone have any thoughts on this?

wapiti crashes my ASP.NET project. Why? How do i fix it?

Heres one scan of Wapiti. I notice when i had images uploaded (users can upload) i get a crash before Launching module crlf. So just using a fresh instance of my site i ran this and got the result below.
My questions are
1. How do i fix the crashes
2. How might i find out what is causing the crash. I used -v 2 to figure out the url and log them in my app. In both cases i dont see any issues and the project crashes outside of my code
3. How so i solve the unicode warning below?
Wapiti-2.2.1 (wapiti.sourceforge.net)
..............................
Notice
========
This scan has been saved in the file C:\unzipped\wapiti-2.2.1\wapiti-2.2.1\src/s
cans/localhost:17357.xml
You can use it to perform attacks without scanning again the web site with the "
-k" parameter
[*] Loading modules :
mod_crlf, mod_exec, mod_file, mod_sql, mod_xss, mod_backup, mod_htaccess
, mod_blindsql, mod_permanentxss, mod_nikto
[+] Launching module crlf
[+] Launching module exec
[+] Launching module file
[+] Launching module sql
C:\unzipped\wapiti-2.2.1\wapiti-2.2.1\src\attack\mod_sql.py:185: UnicodeWarning:
Unicode equal comparison failed to convert both arguments to Unicode - interpre
ting them as being unequal
if (page, tmp) not in self.attackedPOST:
[+] Launching module xss
Traceback (most recent call last):
File "wapiti.py", line 449, in <module>
wap.attack()
File "wapiti.py", line 266, in attack
x.attack(self.urls, self.forms)
File "C:\unzipped\wapiti-2.2.1\wapiti-2.2.1\src\attack\attack.py", line 121, i
n attack
self.attackGET(page, dictio, headers)
File "C:\unzipped\wapiti-2.2.1\wapiti-2.2.1\src\attack\mod_xss.py", line 71, i
n attackGET
self.findXSS(page, {}, "", code, "", payloads, headers["link_encoding"])
File "C:\unzipped\wapiti-2.2.1\wapiti-2.2.1\src\attack\mod_xss.py", line 306,
in findXSS
dat = self.HTTP.send(url).getPage()
File "C:\unzipped\wapiti-2.2.1\wapiti-2.2.1\src\net\HTTP.py", line 94, in send
info, data = self.h.request(target, headers = _headers)
File "C:\unzipped\wapiti-2.2.1\wapiti-2.2.1\src\net\httplib2\__init__.py", lin
e 1084, in request
(response, content) = self._request(conn, authority, uri, request_uri, metho
d, body, headers, redirections, cachekey)
File "C:\unzipped\wapiti-2.2.1\wapiti-2.2.1\src\net\httplib2\__init__.py", lin
e 888, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, he
aders)
File "C:\unzipped\wapiti-2.2.1\wapiti-2.2.1\src\net\httplib2\__init__.py", lin
e 853, in _conn_request
response = conn.getresponse()
File "C:\dev\bin\Python26\lib\httplib.py", line 974, in getresponse
response.begin()
File "C:\dev\bin\Python26\lib\httplib.py", line 391, in begin
version, status, reason = self._read_status()
File "C:\dev\bin\Python26\lib\httplib.py", line 349, in _read_status
line = self.fp.readline()
File "C:\dev\bin\Python26\lib\socket.py", line 397, in readline
data = recv(1)
socket.error: [Errno 10054] An existing connection was forcibly closed by the re
mote host
Wapiti can crash applications because it uses a lot of your application. Wapiti stack traced when doing an XSS test, and I don't think an xss test can crash an application. However, by submitting a lot of 1 type of request, then this could cause a DoS condition. You need to track down the last request that Wapiti made. Wapiti has a verbose mode, I think its -v and it will print out every request it makes. Once you have the file that is crashing you should review it manually.
Wapiti's blind sql injection attack module uses mysql's benchmark() function which WILL DoS your mysql server, I recommend turning this one off if you are have trouble scanning your entire site.

Resources