[Twisted-Python] How to send response 401

Justin Johnson justinjohnson at fastmail.fm
Tue Jun 10 08:50:40 MDT 2003


I tried the following and it seemed to work.  Can someone let me know if
this is the correct way to do this in twisted?

	def render(self, request):
		headers = request.getAllHeaders()
		if headers.has_key("authorization"):
			log("User has authorization key: " + headers["authorization"])
			real_auth = '%s:%s' % (config.adminUser, config.adminPassword)
			auth = headers["authorization"]
			auth = auth.replace("Basic ","")
			decoded_auth = decodestring(auth)
			if decoded_auth == real_auth:
				request.setResponseCode(http.FOUND)
				return xmlrpc.XMLRPC.render(self, request)
		else:
			log("User not authorized")
			request.setResponseCode(http.UNAUTHORIZED)

On Tue, 10 Jun 2003 08:10:50 -0600, "Justin Johnson"
<justinjohnson at fastmail.fm> said:
> I have an xmlrpc service inheriting from xmlrpc.XMLRPC.  I override
> render to allow for basic authentication as show below.  This all works,
> except that I don't know what to call to send response 401 back to the
> client.  self.send_response(401) is how I would do it with the
> SimpleXMLRPCServer, but obviously doesn't work with the twisted xmlrpc
> server.  Can someone point me in the right direction on this?
> 
> Thanks much.
> -Justin
> 
> P.S. - Moshe, I figured out everything else I asked you on the
> python-list about this.  Just need to know how to send response 401 now. 
> Before I wasn't seeing the header, because request.headers doesn't
> contain all headers.  Had to use request.getAllHeaders().
> 
> 
> 	def render(self, request):
> 		headers = request.getAllHeaders()
> 		if headers.has_key("authorization"):
> 			real_auth = '%s:%s' % (config.adminUser, config.adminPassword)
> 			auth = headers["authorization"]
> 			auth = auth.replace("Basic ","")
> 			decoded_auth = decodestring(auth)
> 			if decoded_auth == real_auth:
> 				return xmlrpc.XMLRPC.render(self, request)
> 		else:
> 			self.send_response(401)
> 			self.end_headers()
> 
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> 




More information about the Twisted-Python mailing list