Re: OAuth 2.0 implementation

2012-03-27 Thread Roland Hedberg
And then to complicate the picture you have OpenID Connect which is an attempt 
at
bringing OpenID and OAuth2.0 together.

By the way I have an implementation of OpenID Connect here:

https://github.com/rohe/pyoidc

-- Roland

27 mar 2012 kl. 11:59 skrev Stuart Bishop:

> On Tue, Mar 27, 2012 at 10:11 AM, Ben Finney  
> wrote:
>> Demian Brecht  writes:
>> 
>>> I'm getting close to an alpha release of an OAuth 2.0 implementation
>>> (https://github.com/demianbrecht/py-sanction).
>> 
>> Thank you for doing this work.
>> 
>> As someone who uses OpenID, what can I read about why OAuth is better?
> 
> They are different, and often you need to use both.
> 
> OpenID allows web sites to authenticate someone. It is not really
> useful for anything not an interactive web site. The consuming site
> never gets your keys, it just gets confirmation from the provider that
> the user is who they claim they are and maybe some details that the
> provider chooses to provide such as an email address.
> 
> OAuth is for generating authentication keys that allow a program to
> authenticate as someone and perform operations on their behalf. You
> use OAuth to generate a key so that Foursquare can send messages via
> Twitter on your behalf, or so the Facebook client on your phone can
> access your account without storing your password. You also get
> authentication here, as you can't generate a key without being
> authenticated, but the real reason it is used instead of OpenID is so
> you can keep the key and keep using it to act as the user; you can
> keep using that key until it expires or it is revoked.
> 
> Authentication providers that don't provide a webapi just implement
> OpenID. Big sites like Google and Facebook implement both OpenID (for
> 'log in with your GMail account') and OAuth ('post this message to
> your Facebook wall').
> 
> -- 
> Stuart Bishop 
> http://www.stuartbishop.net/
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Roland

---
With anchovies there is no common ground 
-- Nero Wolfe

-- 
http://mail.python.org/mailman/listinfo/python-list


Slow termination of process

2012-03-27 Thread Roland Hedberg
Hi!

I have an application/a script that is run from another application.
The script is mostly working as a HTTP client but in some cases it also has to 
act as a HTTP server.
Basically just for serving a few files. The files are dynamically created by 
the script when needed.

To accomplish this I'm trying to use subprocess Popen
and an extremely simple web server script which basically contains:

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer((hostname, port), Handler)
httpd.serve_forever()

In the main script I do:

op = Popen(popen_args, stdout=PIPE, stderr=PIPE)

When the main script is done it closes down the HTTP server by doing:

op.terminate()

The problem I have is that if the main script is run again almost immediate 
then the old HTTP server 
process doesn't seem to have released the port yet. So setting up a new server 
fails.

Is there anything I can do to get the port released immediately when the 
tcpserver is terminated.
Or is there another way of doing this that will work better ?

Roland

---
With anchovies there is no common ground 
-- Nero Wolfe

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slow termination of process

2012-03-27 Thread Roland Hedberg
So, I went for the low-hanging fruit and defined my own TCPServer class

class MyTCPServer(SocketServer.TCPServer):
def __init__(self, server_address, RequestHandlerClass):
self.allow_reuse_address = True
SocketServer.TCPServer.__init__(self, server_address,
RequestHandlerClass)

and then

httpd = MyTCPServer((hostname, port), Handler)
httpd.serve_forever()

and this solved my problem!

So, thanks again Chris!

27 mar 2012 kl. 15:55 skrev Chris Angelico:

> On Wed, Mar 28, 2012 at 12:03 AM, Roland Hedberg  wrote:
>> When the main script is done it closes down the HTTP server by doing:
>> 
>>op.terminate()
>> 
>> The problem I have is that if the main script is run again almost immediate 
>> then the old HTTP server
>> process doesn't seem to have released the port yet. So setting up a new 
>> server fails.
> 
> You may wish to consider a more orderly shutdown (send the server a
> signal upon which it shuts itself down), but the simplest and most
> direct solution is to set the SO_REUSEADDR flag.
> 
> http://docs.python.org/library/socket.html?highlight=so_reuseaddr
> 
> I've not actually used the TCPServer class myself, but a cursory
> glance at the docs suggests that it's possible if you subclass it:
> 
> http://docs.python.org/library/socketserver.html#SocketServer.BaseServer.allow_reuse_address
> 
> Chris Angelico
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Roland

---
With anchovies there is no common ground 
-- Nero Wolfe

-- 
http://mail.python.org/mailman/listinfo/python-list


Any available SAML 2.0 tools in Python ?

2005-11-09 Thread Roland Hedberg
Hi!

The subject says it all.

If there isn't anything already available, does anyone know about  
anyone working on something ?

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Problem build python bindings to lasso with swig on mac os x

2005-12-04 Thread Roland Hedberg
Hi!

This involves quite a lot of different system, so I'm not really sure  
who which would be the right list to query.

So, I'm trying a couple, this list being one of them.

I'm trying to build the lasso (http://lasso.entrouvert.org/)  
libraries and what I really want to get at is the Python bindings.
Those binding are done with the help of swig and it fails with the  
following output:

gcc  -o .libs/_lasso.so -bundle  .libs/_lasso_la-lasso_wrap.o  -L/sw/ 
lib -L/Users
/rolandhedberg/src/libxml2-2.6.22 -L/usr/local/lib -L/lib ../ 
lasso/.libs/liblasso
.dylib /usr/lib/libiconv.dylib /usr/lib/libpthread.dylib /usr/lib/ 
libz.dylib /usr
/lib/libm.dylib /usr/lib/libssl.dylib /usr/lib/libcrypto.dylib /usr/ 
local/lib/lib
dl.dylib /usr/lib/libresolv.dylib /usr/local/lib/ 
libgobject-2.0.dylib /usr/lib/li
bc.dylib /usr/local/lib/libglib-2.0.dylib /usr/local/lib/ 
libintl.dylib /usr/local
/lib/libxmlsec1-openssl.dylib /usr/local/lib/libxmlsec1.dylib -ldl / 
usr/local/lib
/libxslt.dylib /usr/local/lib/libxml2.dylib -lpthread -lz /sw/lib/ 
libiconv.dylib
-lm -lssl -lcrypto /usr/local/lib/libsasl2.dylib -Wl,-F. -Wl,-F.
/usr/bin/ld: Undefined symbols:
_PyArg_ParseTuple
_PyCObject_AsVoidPtr
_PyCObject_FromVoidPtrAndDesc
_PyCObject_GetDesc
_PyCObject_Type
_PyDict_SetItemString
_PyErr_NewException
_PyErr_Occurred
_PyErr_SetObject
_PyErr_SetString
_PyExc_IOError
_PyExc_IndexError
_PyExc_MemoryError
_PyExc_NameError
_PyExc_OverflowError
_PyExc_RuntimeError
_PyExc_SyntaxError
_PyExc_SystemError
_PyExc_TypeError
_PyExc_ValueError
_PyExc_ZeroDivisionError
_PyFloat_FromDouble
_PyInt_AsLong
_PyInt_FromLong
_PyModule_AddObject
_PyModule_GetDict
_PyObject_CallObject
_PyObject_GetAttr
_PyObject_SetAttrString
_PyString_FromString
_PyType_Type
_Py_BuildValue
_Py_InitModule4
__Py_NoneStruct
collect2: ld returned 1 exit status
make[1]: *** [_lasso.la] Error 1
make: *** [all-recursive] Error 1

To me it looks like there is a missing library somewhere, but then I  
might be wrong.

I've tried with different versions of swig 1.3.21 - 1.3.27 with the  
same result.
It's the 0.6.3 version of lasso I'm trying to build.

Comments ?

-- Roland

-- 
http://mail.python.org/mailman/listinfo/python-list


Network performance

2005-08-23 Thread Roland Hedberg

Hi!

I need a fast protocol to use between a client and a server, both  
sides written i Python.


What the protocol has to accomplish is extremely simple; the client  
sends a number of lines (actually a RDF) and the server accepts or  
rejects the packet. That's all !


Now, presently I'm using Twisted and XMLRPC ( why so is a long  
history which I will not go into here) and that is a bit to slow for  
my needs. On my present setup it takes close to 200 ms to perform one  
transfer. But since that includes setting up and tearing  down the  
connection I thought I'd try doing something simpler so I wrote a  
server and a client using socket.


The client sends a number of lines (each ending with \n) and ends one  
set of lines with a empty line.
When the client sends a line with only a "." it means "I'm done close  
the connection".


Letting the client open the connection and sending a number of sets I  
was surprised to find that the performance was equal to what Twisted/ 
XMLRPC did. Around 200 ms per set, not significantly less.


So what can be done to speed up things or is Python incapable of  
supporting fast networking (just a teaser).


-- Roland 

#!/usr/bin/env python

from SocketServer import ThreadingMixIn, TCPServer, StreamRequestHandler

class UdsRequestHandler( StreamRequestHandler ):
	def handle(self):
		rdf = []
		while 1:
			tri = self.rfile.readline().strip()
			if tri == ".":
if rdf:
	print rdf
	self.wfile.write("ACCEPT")
break
			if tri:
rdf.append(tri)
			else:
print rdf
self.wfile.write("ACCEPT")
rdf = []

class UdsServer(ThreadingMixIn, TCPServer):
	allow_reuse_address = 1

if __name__ == "__main__":
	serveraddr = ('', 8765)
	srv = UdsServer(serveraddr, UdsRequestHandler)
	srv.serve_forever()

#!/usr/bin/env python

import socket

# Ending \n vital !!
RDF = """  "primula" .
  "4182" .
  "NIN:19720412-7802" .
  _:a .
_:a  "anst1" .
_:a  "100" .
_:a  "7528" .
_:a  "staff" .
"""

class UdsClient:
	def __init__(self, ip, port):
		self.ip = ip
		self.port = port
		self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
		try:
			self.s.connect((self.ip, self.port))
		except:
			print "Connection error"
			return None

	def send( self, rdf ):
		self.s.send( rdf )
		self.s.send( "\n" )

	def close( self ):
		self.s.send( ".\n" )

	def read( self ):
		r = ""
		try:
			r = self.s.recv(256)
		except:
			print "No response"
		return r

if __name__ == "__main__":
	uc = UdsClient("localhost", 8765)
	n = 10

	while n:
		uc.send( RDF )
		print "Response: %s" % uc.read()
		n -= 1

	uc.close()
	print "DONE"


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Network performance

2005-08-23 Thread Roland Hedberg

23 aug 2005 kl. 10.14 skrev Michael Sparks:

> Roland Hedberg wrote:
>
>> I was surprised to find that the performance was equal to what
>> Twisted/XMLRPC did. Around 200 ms per set, not significantly less.
>>
>
> That should tell you two things:
>* Twisted/XMLRPC is as efficient as you can hand craft. (which is a
>  good use reason for using it).

I already gathered that much :-)

>* That what you're measuring is overhead - and most likely of  
> setup.

Not necessarily! If the number of client - server queries/responses  
are large enough the effect of the setup time should be negligible.  
Or making testes with different numbers of queries you should be able  
to deduce the setup time.

> I'd measure the ping time between your two hosts.
>
> If your ping time is significantly lower -  eg you're running on
> localhost - I'd suggest you translate your code to C (and/or post
> your code),

I did the tests on localhost!

And I did post the code!

So, I made another test. I used  a server I have already written in C  
and which I know quite well how fast it is.

Using a python client I've written that talks to this server, it  
takes 0.8 s for the python client to start, connect and send
1000 queries. A C client is a bit faster but not a lot.

This is more in the order of what I'd like to have.

Hmm, not surprising this makes me suspect my python server  
implementation :-/

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Network performance

2005-08-23 Thread Roland Hedberg

23 aug 2005 kl. 15.14 skrev Sion Arrowsmith:

> Roland Hedberg  <[EMAIL PROTECTED]> wrote:
>
>
> The easy solutions are to either change:
>
>
>> def send( self, rdf ):
>> self.s.send( rdf )
>> self.s.send( "\n" )
>>
>
> to
>
> def send( self, rdf ):
> self.s.send( rdf+"\n" )
>
> or to self.s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 0) after
> connecting self.s .

I had just caught this but didn't really know way yet!

> What's going on is something like this:

Excellent description !

Thanks ever so much !!

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


DB problem while trying to use Shelve

2004-11-30 Thread Roland Hedberg
Hi!
Want to use shelve in a program I'm writing, but I encounter a problem.
This is the tail of the traceback I get back:
  File "/sw/lib/python2.3/shelve.py", line 231, in open
return DbfilenameShelf(filename, flag, protocol, writeback, binary)
  File "/sw/lib/python2.3/shelve.py", line 212, in __init__
Shelf.__init__(self, anydbm.open(filename, flag), protocol, 
writeback, binary)
  File "/sw/lib/python2.3/anydbm.py", line 83, in open
return mod.open(file, flag, mode)
  File "/sw/lib/python2.3/dbhash.py", line 16, in open
return bsddb.hashopen(file, flag, mode)
  File "/sw/lib/python2.3/bsddb/__init__.py", line 186, in hashopen
d.set_flags(hflags)
bsddb._db.DBInvalidArgError: (22, 'Invalid argument')
Illegal instruction

This is on a MacOSX machine with python 2.3.3 and db 4.2
What's going on ?
And what can I do about it ?
-- Roland


smime.p7s
Description: S/MIME cryptographic signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Weird connection problem

2014-10-25 Thread Roland Hedberg
When I try to access a URL using requests I always get: 
socket.error: [Errno 104] Connection reset by peer

If I try to access the same URL using curl I get no error message instead I get 
the page.
The same result if I use a web browser like Safari.

But, if I use python httplib I also get Errno 104.

What gives ?

Oh, by the way it’s a HTTPS url.

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Weird connection problem

2014-10-25 Thread Roland Hedberg
Oh, by the way!
To make this more interesting :-/

I saw this behavior on a Linux machine (Ubuntu 14.04 LTS) using Python 2.7.6 if 
I do the same exercise 
on a Mac OS X machine also with Python 2.7.6 - no problem what so ever.

> 25 okt 2014 kl. 08:40 skrev Roland Hedberg :
> 
> When I try to access a URL using requests I always get: 
> socket.error: [Errno 104] Connection reset by peer
> 
> If I try to access the same URL using curl I get no error message instead I 
> get the page.
> The same result if I use a web browser like Safari.
> 
> But, if I use python httplib I also get Errno 104.
> 
> What gives ?
> 
> Oh, by the way it’s a HTTPS url.
> 
> — Roland
> 
> ”Being able to think like a child is an important attribute of being an 
> adult” - Eddie Izzard
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Weird connection problem

2014-10-25 Thread Roland Hedberg
It’s a special HTTPS url and searching further it seems to be a SNI problem 
talked about here:

http://stackoverflow.com/questions/18578439/using-requests-with-tls-doesnt-give-sni-support

> 25 okt 2014 kl. 08:48 skrev Joel Goldstick :
> 
> On Sat, Oct 25, 2014 at 9:40 AM, Roland Hedberg  wrote:
>> When I try to access a URL using requests I always get:
>> socket.error: [Errno 104] Connection reset by peer
>> 
>> If I try to access the same URL using curl I get no error message instead I 
>> get the page.
>> The same result if I use a web browser like Safari.
>> 
>> But, if I use python httplib I also get Errno 104.
>> 
>> What gives ?
>> 
>> Oh, by the way it’s a HTTPS url.
>> 
>> — Roland
> 
> Is this for any url or a specific one?
>> --
>> https://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> -- 
> Joel Goldstick
> http://joelgoldstick.com
> -- 
> https://mail.python.org/mailman/listinfo/python-list

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Which OAuth library?

2014-09-02 Thread Roland Hedberg

14 aug 2014 kl. 19:54 skrev Chris “Kwpolska” Warrick :

> On 14 August 2014 18:51 "Richard Prosser"  wrote:
> >
> > I "need" one for use with Flask, as I don't really have time to implement 
> > my own.
> 
> You should not implement things on your own if there are existing and same 
> implementations.
> 
> > Initially this will be for the "Two-Legged" case but I may well have to 
> > support the "Three-Legged" version later on. "Open ID Connect" may also be 
> > an option eventually.
> >
> > The basic idea is to provide an authorization/authentication service in a 
> > fairly conventional manner. My boss has told me to use OAuth, probably 
> > because he has experience with it and also to allow for third-party 
> > transactions.
> >
> > However it is not clear to me how I should decide between the various 
> > packages on offer. Any advice from experienced/informed users would be very 
> > welcome.

I’m responsible for one implementation (pyoidc) that is primary a OpenID 
connect implementation.
But since OpenID Connect is a profile of OAuth2 it will work in an OAuth2 
context too.

This implementation is special in that it’s the de facto reference 
implementation for OpenID Connect.
It’s that, due to the fact that I’ve built the OpenID Connect test suit which 
most today available
OpenID Provider implementations have verified themselves against.

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

-- 
https://mail.python.org/mailman/listinfo/python-list


Weird SSL problem

2014-09-29 Thread Roland Hedberg
Hi!

I’m trying to access 
https://stsadweb.one.microsoft.com/adfs/.well-known/openid-configuration

Doing it the simplest way I get the following:

>>> import urllib
>>> f = 
>>> urllib.urlopen("https://stsadweb.one.microsoft.com/adfs/.well-known/openid-configuration";)
Traceback (most recent call last):
  File "", line 1, in 
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", 
line 87, in urlopen
return opener.open(url)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", 
line 208, in open
return getattr(self, name)(url)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", 
line 437, in open_https
h.endheaders(data)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", 
line 969, in endheaders
self._send_output(message_body)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", 
line 829, in _send_output
self.send(msg)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", 
line 791, in send
self.connect()
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", 
line 1176, in connect
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 
387, in wrap_socket
ciphers=ciphers)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 
143, in __init__
self.do_handshake()
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 
305, in do_handshake
self._sslobj.do_handshake()
IOError: [Errno socket error] [Errno 54] Connection reset by peer
>>> import ssl
>>> ssl.OPENSSL_VERSION
’OpenSSL 0.9.8za 5 Jun 2014'

Now, using Safari, or curl for that matter, from the same machine works without 
a hitch.

The URL above is also the only URL I’ve encountered this problem with.

Anyone got an idea ?

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Weird SSL problem

2014-10-01 Thread Roland Hedberg

30 sep 2014 kl. 00:55 skrev Ned Deily :

> In article ,
> Roland Hedberg  wrote:
> 
>> Hi!
>> 
>> I¹m trying to access 
>> https://stsadweb.one.microsoft.com/adfs/.well-known/openid-configuration
>> 
>> Doing it the simplest way I get the following:
>> 
>>>>> import urllib
>>>>> f = 
>>>>> urllib.urlopen("https://stsadweb.one.microsoft.com/adfs/.well-known/openid
>>>>> -configuration")
>> Traceback (most recent call last):
>>  File "", line 1, in 
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py",
>>   line 87, in urlopen
>>return opener.open(url)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py",
>>   line 208, in open
>>return getattr(self, name)(url)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py",
>>   line 437, in open_https
>>h.endheaders(data)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py"
>>  , line 969, in endheaders
>>self._send_output(message_body)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py"
>>  , line 829, in _send_output
>>self.send(msg)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py"
>>  , line 791, in send
>>self.connect()
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py"
>>  , line 1176, in connect
>>self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", 
>>  line 387, in wrap_socket
>>ciphers=ciphers)
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", 
>>  line 143, in __init__
>>self.do_handshake()
>>  File 
>>  "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", 
>>  line 305, in do_handshake
>>self._sslobj.do_handshake()
>> IOError: [Errno socket error] [Errno 54] Connection reset by peer
>>>>> import ssl
>>>>> ssl.OPENSSL_VERSION
>> ¹OpenSSL 0.9.8za 5 Jun 2014'
>> 
>> Now, using Safari, or curl for that matter, from the same machine works 
>> without a hitch.
>> 
>> The URL above is also the only URL I¹ve encountered this problem with.
>> 
>> Anyone got an idea ?
> 
> I believe the problem is that the connection is protected by a 
> multi-hostname server certificate and Python 2's urllib (and underlying 
> httplib and ssl modules) do not support SNI extensions to TLS.  The 
> request above works fine with Python 3 (which has supported client-side 
> SNI since Python 3.2).  See http://bugs.python.org/issue5639 for more 
> discussion of the matter.  If Python 3 is not an option for you, the 
> requests package available via PyPI should help.

You’re absolutely correct in that it’s a SNI problem.
Python 3 is not an option and I was already using requests obviously missing 
something.
Ah, had to install some extra libraries.

— Roland

”Being able to think like a child is an important attribute of being an adult” 
- Eddie Izzard

-- 
https://mail.python.org/mailman/listinfo/python-list


Support for Galois/Counter Mode (GCM) ?

2011-11-01 Thread Roland Hedberg
Hi !

Is there a crypto library for Python that has support for GCM ??

-- Roland



-- 
http://mail.python.org/mailman/listinfo/python-list


import order or cross import

2007-01-03 Thread Roland Hedberg
Hi!

I'm having a bit of a problem with import.

I'm writing a marshalling system that based on a specification will
create one or more files containing mostly class definitions.

If there are more than one file created (and there are reasons for
creating more than one file in some instances) then they will import
each other since there may be or is interdependencies in between them.

And this is where the going gets tough.

Let's assume I have the following files:

- ONE.py 

import TWO

class Car:
def __init__(self):
self.color = None

def set_color(self,v):
if isinstance(v,TWO.Black) or isinstance(v,TWO.White):
self.color = v

class BaseColor:
def __init__(self):
pass
def __str__(self):
return self.color

if __name__ == "__main__":
car = Car()
color = TWO.Black()
car.set_color(color)
print car.color

-- TWO.py ---

import ONE

class Black(ONE.BaseColor):
color = "Black"
def __init__(self):
ONE.BaseColor.__init__(self)

class White(ONE.BaseColor):
color = "White"
def __init__(self):
ONE.BaseColor.__init__(self)

-

Now, running ONE.py causes no problem it will print "Black", but running
TWO.py I get:
AttributeError: 'module' object has no attribute 'BaseColor'

So, how can this be solved if it can be ?

To join ONE.py and TWO.py into one file could be a solution if there
where no other conditions (non-language based) that prevented it.

-- Roland

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import order or cross import

2007-01-03 Thread Roland Hedberg
Duncan Booth wrote:

> 
> Remember that all statements in Python are executed at the time they are 
> encountered: there are no declarations (apart from 'global') so no looking 
> ahead to see what classes or functions are coming up.

Yes, I've seen this time and time again.

> One other complication in your particular instance: when you run a ONE.py 
> as a script the script is loaded in a module called '__main__', so 'import 
> ONE' will actually load a separate module which is NOT the same as the 
> __main__ module. Likewise when you run TWO.py as a script you have three 
> modules __main__ (loaded from TWO.py), ONE, and TWO.

Hmm, that's interesting.

> 
> The fix in this sort of case is usually to extract the base class out to a 
> third module. If you put BaseColor in base.py then you can safely import 
> that anywhere you want it.

OK, so I'll go for this option. Takes some more intelligence in the
marshalling code but it should be doable.

> An alternative in this case would be to edit ONE.py and move the line 
> 'import TWO' down below the definition of BaseColor: nothing before that 
> actually requires TWO to have been imported yet.

Since no person is involved, in creating files like ONE.py and TWO.py,
this also has to be done by the marshalling code.
I've no clear idea which alternative would be best/easiest but your
first one seems clearer.

> However, you really should try to separate scripts from modules otherwise 
> the double use as both __main__ and a named module is going to come back 
> and bite you.

Oh, that was only there as an example, so you could see the intent usage.

Thanks Duncan!

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import order or cross import

2007-01-03 Thread Roland Hedberg
Jussi Salmela wrote:
> Roland Hedberg kirjoitti:

>> I'm having a bit of a problem with import.
>>
>> I'm writing a marshalling system that based on a specification will
>> create one or more files containing mostly class definitions.

> Maybe I'm missing something, but why is the class BaseColor in file 
> ONE.py and not in TWO.py?

This has to do with the how the specifications that the marshallings
system works with are structured. And it is therefor not something I can
control.

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Other classes in a module

2007-03-25 Thread Roland Hedberg
Hi!

Can an instance of a class in a module, in any simple way find out which
other classes that exists in said module ?

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Any python implementation of XML-DSIG ?

2008-02-05 Thread Roland Hedberg
Or is XMLsig for Dynamic Languages (Ruby, Python, PHP and Perl)
at http://xmlsig.sourceforge.net/ the only option ?

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classobj() question

2008-02-12 Thread Roland Hedberg
Peter Otten wrote:
> Roland Hedberg wrote:
> 
>> I'm in the position that I have a bunch of classes defined before hand
>> and then in some special circumstances I need to dynamically create a
>> class that has a number of the static classes as parents.
>>
>> So I thought I could use classobj() from the new module, it seem exactly
>> what I wanted.
>>
>> But, it doesn't perform as I expected.
>>
>> I've made an extremely simple program to try to show what I mean and
>> what I expected. It's attached to this mail.
>>
>> So, how should I have done it ?
> 
> (1) You are working with newstyle classes -- use type() instead of
> new.classobj().
> (2) Your inheritance tree has diamonds in it -- call baseclass initializers
> using super() in *all* classes, e. g:
> 
> class A(object):
> def __init__(self):
> super(A, self).__init__()
> # your code
> 
> Python will then take care that every initializer in the inheritance tree is
> called once.

Thanks Peter, your hints was exactly what I needed. Now it works !

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


classobj() question

2008-02-12 Thread Roland Hedberg
Hi!

I'm in the position that I have a bunch of classes defined before hand
and then in some special circumstances I need to dynamically create a
class that has a number of the static classes as parents.

So I thought I could use classobj() from the new module, it seem exactly
what I wanted.

But, it doesn't perform as I expected.

I've made an extremely simple program to try to show what I mean and
what I expected. It's attached to this mail.

So, how should I have done it ?

-- Roland
#!/usr/bin/env python 

class A(object):
	def __init__(self):
		if "list" in self.__dict__.keys():
			self.list.extend(["a","b","c"])
		else:
			self.list = ["a","b","c"]
		self.list.sort()

class B(object):
	def __init__(self):
		if "list" in self.__dict__.keys():
			self.list.extend(["x","y","z"])
		else:
			self.list = ["x","y","z"]
		self.list.sort()

class C(B):
	def __init__(self):
		B.__init__(self)
		if "list" in self.__dict__.keys():
			self.list.extend(["0","1","2"])
		else:
			self.list = ["0","1","2"]
		self.list.sort()

class X(C):
	def __init__(self):
		C.__init__(self)
		if "list" in self.__dict__.keys():
			self.list.extend(["9","8","7"])
		else:
			self.list = ["9","8","7"]
		self.list.sort()

__test__ = { "a":"""
>>> from new import classobj
>>> c = C()
>>> c.list
['0', '1', '2', 'x', 'y', 'z']
>>> x = X()
>>> x.list
['0', '1', '2', '7', '8', '9', 'x', 'y', 'z']
>>> D = classobj("D",(A,C),{})
>>> d = D()
>>> d.list
['0', '1', '2', '7', '8', '9', 'x', 'y', 'z']
>>> E = classobj("E",(C,A),{})
>>> e = E()
>>> e.list
['0', '1', '2', '7', '8', '9', 'x', 'y', 'z']
"""}

if __name__ == "__main__":
	import doctest
	doctest.testmod()

-- 
http://mail.python.org/mailman/listinfo/python-list

json-rpc in Python

2009-04-15 Thread Roland Hedberg

Hi!

Anyone got information on python implementations of JSON-RPC ?

I'd like to run a jsonrpc server using mod_python in an Apache server.

I know about, and have used for a while, Jan-Klaas Kollhof's  
implementation which used to be at http://json-rpc.org/, but isn't  
accessible anymore.


Therefor I have started to look elsewhere.

I have found a couple of implementations:

http://www.simple-is-better.org/rpc/
looks OK but seems to be built to work as a stand-alone server

http://www.freenet.org.nz/dojo/pyjson/
should work with 2.6 ? Is it maintained ?

http://python.cx.hu/qxjsonrpc/
Which according to the documentation does not conform to the JSON-RPC  
specification


Have I missed any ?

What are people using ?

-- Roland


--
http://mail.python.org/mailman/listinfo/python-list


Problem with Elementtree and XMLSchem instance type

2010-07-30 Thread Roland Hedberg
Hi!

I have the following XML snippet:

http://www.w3.org/2001/XMLSchema-instance";
xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706";
xsi:type="fed:SecurityTokenServiceType">



This part after parsing with Elementtree gives me an Element instance
with the following properties:

> tree.tag
{urn:oasis:names:tc:SAML:2.0:metadata}RoleDescriptor
> tree.keys()
['{http://www.w3.org/2001/XMLSchema-instance}type']
> tree['{http://www.w3.org/2001/XMLSchema-instance}type']
fed:SecurityTokenServiceType

And there is the problem, I've lost the coupling between the prefix
'fed' and the namespace
"http://docs.oasis-open.org/wsfed/federation/200706";.

Is there any way I can get at the prefix <-> namespace mapping from an
Element instance ?
I've read the documentation I can find and there is nothing that tells
me how to get at the mapping.

If I print the Element instance the prefix 'fed' is replace by 'ns0' or
something like that.
Definitely something that has no connection to the original 'fed'.

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


A zlib question

2009-07-10 Thread Roland Hedberg

Hi!

I have a problem with zlib and compressing/decompressing according to  
RFC 1951.


It seems like I can decompress, something compressed according to RFC  
1951 by someone else, provided I set wbits to something negative (used  
-8 but I guess any negative number would work?).


But how can I compress using zlib so it doesn't add a gzip header ?

-- Roland 
--

http://mail.python.org/mailman/listinfo/python-list


lxml and xmlsec

2009-09-25 Thread Roland Hedberg

Hi!

Anyone know if it is possible to use xmlsec together with lxml ?

-- Roland
--
http://mail.python.org/mailman/listinfo/python-list


SAML2 support in Python

2010-10-06 Thread Roland Hedberg
Hi!

For those that are interested in making there web applications work in a SAML2 
federated environment I have two things to offer:

1) PySAML2 - a not complete but working implementation of SAML2 in Python. 
Started with implementing the SP part and are now slowly doing the IdP 
part too.

https://code.launchpad.net/~roland-hedberg/pysaml2/main

For those using Django you should take notice of what Lorenzo Gil 
Sanchez is doing using PySAML2.
https://code.launchpad.net/~lgs/pysaml2/main

2) IdPproxy - a gateway between social media and the SAML2 world.
To the SPs in a SAML2 federation it looks like a normal IdP, but it 
allows the user to authenticate using
Twitter, Facebook, Google, OpenID and Windows Live ID.
Work in progress, alpha quality.

https://code.launchpad.net/~roland-hedberg/+junk/IdPproxy

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list