小编Dan*_*Dan的帖子

使用MultipartPostHandler使用Python POST表单数据

问题:使用Python的urllib2发布数据时,所有数据都经过URL编码并作为Content-Type发送:application/x-www-form-urlencoded.上传文件时,应将Content-Type设置为multipart/form-data,并将内容编码为MIME.这个问题的讨论在这里:http: //code.activestate.com/recipes/146306/

为了解决这个限制,一些敏锐的程序员创建了一个名为MultipartPostHandler的库,它创建了一个OpenerDirector,您可以使用urllib2来主要使用multipart/form-data自动POST.此库的副本位于:http: //peerit.blogspot.com/2007/07/multipartposthandler-doesnt-work-for.html

我是Python的新手,无法让这个库工作.我基本上写了下面的代码.当我在本地HTTP代理中捕获它时,我可以看到数据仍然是URL编码的,而不是多部分MIME编码.请帮我弄清楚我做错了什么或更好的方法来完成这件事.谢谢 :-)

FROM_ADDR = 'my@email.com'

try:
    data = open(file, 'rb').read()
except:
    print "Error: could not open file %s for reading" % file
    print "Check permissions on the file or folder it resides in"
    sys.exit(1)

# Build the POST request
url = "http://somedomain.com/?action=analyze"       
post_data = {}
post_data['analysisType'] = 'file'
post_data['executable'] = data
post_data['notification'] = 'email'
post_data['email'] = FROM_ADDR

# MIME encode the POST payload
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(opener)
request = urllib2.Request(url, post_data)
request.set_proxy('127.0.0.1:8080', …
Run Code Online (Sandbox Code Playgroud)

python upload multipartform-data file urllib2

47
推荐指数
3
解决办法
6万
查看次数

URL在Python中编码非值对

我正在尝试在Python中使用Google的AJAX(JSON)Web搜索API.我被卡住了,因为Python的urllib.urlencode()只接受值对,而不是字符串本身,以进行编码.在Google的API中,查询字符串是搜索字词,它不与变量关联.

query = "string that needs to be encoded"
params = urllib.urlencode(query) # THIS FAILS
# http://code.google.com/apis/ajaxsearch/documentation/reference.html
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&%s&%s" % (params, GOOGLE_API_KEY)

request = urllib2.Request(url)
request.add_header('Referer', GOOGLE_REFERER)

search_results = urllib2.urlopen(request)
raw_results = search_results.read()
json = simplejson.loads(raw_results)
estimatedResultCount = json['responseData']['cursor']['estimatedResultCount']

if estimatedResultCount != 0:
    print "Google: %s hits" % estimatedResultCount
Run Code Online (Sandbox Code Playgroud)

如何对我的搜索字词进行urlencode?

python json urlencode

14
推荐指数
1
解决办法
1万
查看次数

哪些Python贝叶斯文本分类模块与dbacl类似?

快速谷歌搜索显示,有很多贝叶斯分类器被实现为Python模块.如果我想要包装,类似于dbacl的高级功能,哪些模块适合我?

训练

% dbacl -l one sample1.txt
% dbacl -l two sample2.txt
Run Code Online (Sandbox Code Playgroud)

分类

% dbacl -c one -c two sample3.txt -v
one
Run Code Online (Sandbox Code Playgroud)

python text classification bayesian

11
推荐指数
1
解决办法
5139
查看次数

Django一对多模型

以下模型描述了漏洞以及引用该漏洞的Internet上的URL.假设每个URL只讨论1个漏洞,并且许多URL将讨论该漏洞.这是布局模型的正确方法吗?

class Vuln(models.Model):
  pub_date = models.DateTimeField("Publication Date")
  short_description = models.CharField("Description", max_length=70)

  reference_urls = models.ForeignKey(Url, unique=True, blank=True, verbose_name="Reference URLs")
  vendor = models.ForeignKey(Vendor, verbose_name="Vendor")

class Url(models.Model):
  url = models.URLField("URL", max_length=200)
Run Code Online (Sandbox Code Playgroud)

Admin应用程序为参考URL提供了一个"选择"框,这不是我想要的.当我添加新的漏洞对象时,已输入的所有现有URL都显示在该下拉列表中,这又是不自然的.我觉得这应该与博客评论的方式非常相似,即.评论适用于单个博客条目,而不是其他任何一个博客条目可能有很多评论.我如何在Django模型中表达这一点?

python django model one-to-many

11
推荐指数
1
解决办法
1万
查看次数