Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Mechanize + GAEpython code

I am aware of previous questions regarding mechanize + Google App Engine, What pure Python library should I use to scrape a website? and Mechanize and Google App Engine.

Also there is some code here, which I cannot get to work on app engine, throwing

File “D:\data\eclipse-php\testpy4\src\mechanize\_http.py”, line 43, in socket._fileobject(”fake socket”, close=True)
File “C:\Program Files (x86)\Google\google_appengine\google\appengine\dist\socket.py”, line 42, in _fileobject
fp.fileno = lambda: None
AttributeError: ’str’ object has no attribute ‘fileno’
INFO 2009-12-14 09:37:50,405 dev_appserver.py:3178] “GET / HTTP/1.1″ 500 -

Is anybody willing to share their working mechanize + appengine code?

like image 986
Lydon Ch Avatar asked Dec 14 '09 16:12

Lydon Ch


People also ask

What is mechanize in Python?

The mechanize module in Python is similar to perl WWW:Mechanize. It gives you a browser like object to interact with web pages. Here is an example on how to use it in a program.

How do I install mechanize in Python?

To install for development: git clone https://github.com/python-mechanize/mechanize.git cd mechanize pip3 install -e . To install manually, simply add the mechanize sub-directory somewhere on your PYTHONPATH .


1 Answers

I have solved this problem, just change the code of mechanize._http.py, about line 43, from:

try:
    socket._fileobject("fake socket", close=True)
except TypeError:
    # python <= 2.4
    create_readline_wrapper = socket._fileobject
else:
    def create_readline_wrapper(fh):
        return socket._fileobject(fh, close=True)

to:

try:
    # fixed start -- fixed for gae
    class x:
        pass

    # the x should be an object, not a string,
    # This is the key
    socket._fileobject(x, close=True)
    # fixed ended
except TypeError:
    # python <= 2.4
    create_readline_wrapper = socket._fileobject
else:
    def create_readline_wrapper(fh):
        return socket._fileobject(fh, close=True)
like image 152
Michael Avatar answered Oct 21 '22 16:10

Michael