1

I have this code, that supposed to work but I'm getting strange errors, for other user this code works fine.

# -*- coding: utf-8 -*-

import re, sys
import urllib2
import urllib2_file


user_hash='MTggMzc6T1dZgggggzWXpWbVptggggHTXlOV1F5WWgggggggWT0%3D'
text_file = 'sveikinimas.txt'

postdata = { 'type':    '40',
             'description': '',
             'descr': 'Pelėsiais ir kerpėm apaugus aukštai\nTrakų štai garbinga pilis\n....',
             'filetype': '2',
             'name': 'Su šventėmis!',
             'file': {'fd': open(text_file), 'filename': text_file},
             'nfo': ''             
     }

req = urllib2.Request('http://www.linkomanija.net/takefreak.php',postdata)
req.add_header('Cookie', 'login=' + user_hash)
print req
response = urllib2.urlopen(req)
print response
html = response.read()
f = open("out.html", "wb")
f.write(html)
f.close()

This code works for another user, but I'm getting strange error:

<urllib2.Request instance at 0x0387C698>
Traceback (most recent call last):
  File "C:\Users\drakaz\Desktop\lm\hello.py", line 25, in <module>
    response = urllib2.urlopen(req)
  File "C:\Python26\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python26\lib\urllib2.py", line 392, in open
    response = self._open(req, data)
  File "C:\Python26\lib\urllib2.py", line 410, in _open
    '_open', req)
  File "C:\Python26\lib\urllib2.py", line 370, in _call_chain
    result = func(*args)
  File "C:\Users\drakaz\Desktop\lm\urllib2_file.py", line 207, in http_open
    return self.do_open(httplib.HTTP, req)
  File "C:\Users\drakaz\Desktop\lm\urllib2_file.py", line 298, in do_open
    return self.parent.error('http', req, fp, code, msg, hdrs)
  File "C:\Python26\lib\urllib2.py", line 436, in error
    return self._call_chain(*args)
  File "C:\Python26\lib\urllib2.py", line 370, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\urllib2.py", line 519, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error -1:
4
  • Can you please format your code. Use the Editor provided by SO. Commented Dec 29, 2010 at 11:11
  • @marcog: Thanks for the formatting. Jack, the error message shows something from urllib2_file, but your code does not show any call from that. Why don't you comment that import statement and try? Commented Dec 29, 2010 at 11:19
  • Sounds like a problem on the server-side. Do you run the server? Commented Dec 29, 2010 at 11:20
  • It can't be server problem, that server is not mine. For other user this code works. So it must be something that I'm using maybe python version or that urllib2_file is wrong. Commented Dec 29, 2010 at 12:14

1 Answer 1

1

The error message is from urllib2_file. I believe, it is not proper. You may just wish to comment it and try if it that module is not being used.

I also see that in the post data, you are having another dictionary inside. It should not be the case. Just have the contents of the file. Read the file and have it the content as the string. No dictionaries, no file names.

Sign up to request clarification or add additional context in comments.

2 Comments

Tried commenting it, but that module is used I posted the error after commenting that module.
Don't use this: 'file': {'fd': open(text_file), 'filename': text_file}, . It is incorrect. Just read the file and pass the content as the file.

Your Answer

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.