0

I have the next situation. I try to overwrite method send_error in BaseHTTPRequestHandler in BaseHTTPServer.py file. BaseHTTPServer.py has such structure concerning send_error method:

def _quote_html(html):
 blah
 blah
class HTTPServer():
 blah
 blah
class BaseHTTPRequestHandler():
 blah
 blah
 def send_error(self):
 blah
 blah
 content = (self.error_message_format %
 {'code': code, 'message': _quote_html(message), 'explain': explain})

Here inside send_error method _quote_html function is called. It works inside BaseHTTPServer.py file but if create my own httphandler, inherit from BaseHTTPRequestHandler and try to overwrite send_error, my function send_error can not access _quote_html function located in BaseHTTPServer.py file outside of BaseHTTPRequestHandler class:

Traceback (most recent call last):
 File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
 self.finish_request(request, client_address)
 File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
 self.RequestHandlerClass(request, client_address, self)
 File "/python_codes/Junior/Level1/C&C/HttpServer/HttpHandler.py", line 12, in __init__
 BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
 File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
 self.handle()
 File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
 self.handle_one_request()
 File "/python_codes/Junior/Level1/C&C/HttpServer/HttpHandler.py", line 26, in handle_one_request
 if not self.parse_request():
 File "/usr/lib/python2.7/BaseHTTPServer.py", line 286, in parse_request
 self.send_error(400, "Bad request syntax (%r)" % requestline)
 File "/python_codes/Junior/Level1/C&C/HttpServer/HttpHandler.py", line 106, in send_error
 {'code': code, 'message': _quote_html(message), 'explain': explain})
NameError: global name '_quote_html' is not defined 

So my question is how can I access function outside parent class in module file? In my case from send_error() to _quote_html(). Everythong is imported from BaseHTTPServer.py:

from BaseHTTPServer import *
asked Jan 30, 2015 at 12:20
2
  • How are you inheriting from BaseHttpRequestHandler... ? Commented Jan 30, 2015 at 12:26
  • As usually: class myhandler(BaseHttpRequestHandler): Commented Jan 30, 2015 at 12:40

2 Answers 2

1

Rename your function into quote_html. Or do explicit import like this:

from BaseHTTPServer import _quote_html

Since:

from M import * does not import objects whose name starts with an underscore.

answered Jan 30, 2015 at 12:23
Sign up to request clarification or add additional context in comments.

3 Comments

I second the explicit import suggestion. from <module> import * is generally frowned upon since it pollutes the local namespace and makes it hard to track an item's origin.
@ВладХристенко Though... its fine even to accept... but please do both - "upvote and accept" the right answer.
I currently do not have enough reputation to upvote((( I would do it.
0

I have tried this and it works fine:

def b():
 return 45
class Person(object):
 def getYears(self):
 return self.b()
print Person().getYears()

So, the only difference I see is the 'objects' between brackets.

answered Jan 30, 2015 at 12:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.