簡體   English   中英

Python urllib2 http -1錯誤

[英]Python urllib2 http -1 error

我有此代碼,該代碼可以正常工作,但出現了奇怪的錯誤,對於其他用戶,此代碼可以正常工作。

# -*- 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()

這段代碼適用於其他用戶,但出現了奇怪的錯誤:

<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:

錯誤消息來自urllib2_file 我相信這是不合適的。 您可能只希望對其進行注釋,然后嘗試不使用該模塊。

我還看到,在發布數據中,您內部還有另一個字典。 事實並非如此。 只需擁有文件的內容。 讀取文件,並將其內容作為字符串。 沒有字典,沒有文件名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM