小编ome*_*ach的帖子

如何指定python请求http put body?

我正在尝试使用requests模块重写一些旧的python代码.目的是上传附件.邮件服务器需要以下规范:

https://api.elasticemail.com/attachments/upload?username=yourusername&api_key=yourapikey&file=yourfilename
Run Code Online (Sandbox Code Playgroud)

有效的旧代码:

h = httplib2.Http()        
        resp, content = h.request('https://api.elasticemail.com/attachments/upload?username=omer&api_key=b01ad0ce&file=tmp.txt', 
        "PUT", body=file(filepath).read(), 
        headers={'content-type':'text/plain'} )
Run Code Online (Sandbox Code Playgroud)

没有找到如何在请求中使用正文部分.

我设法做了以下事情:

 response = requests.put('https://api.elasticemail.com/attachments/upload',
                    data={"file":filepath},                         
                     auth=('omer', 'b01ad0ce')                  
                     )
Run Code Online (Sandbox Code Playgroud)

但不知道如何使用文件内容指定正文部分.

谢谢你的帮助.奥马尔.

python http put httplib2 python-requests

29
推荐指数
2
解决办法
7万
查看次数

在zeromq pub sub上丢失了消息

我正在尝试使用zeromq框架实现pub子设计模式.我们的想法是推出订阅者,然后推出发布者.订阅者将收听100条消息,发布者将发布100条消息.到目前为止一切都那么好......然而实际发生的事情是,即使发布者发布时订户已经启动并运行,订阅者也不会收到所有消息(订阅者将收到100条消息,如果出版商将发送至少500条消息).似乎发布者发送的第一条消息不会发送给订阅者.

有任何想法吗?

提前谢谢,奥梅尔.

订阅者代码(在发布者之前发布)

int i=0;
zmq::context_t context (1);
zmq::socket_t subscriber (context, ZMQ_SUB);
subscriber.connect("tcp://localhost:5556");
subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);

for (int update_nbr = 0; update_nbr < 100; update_nbr++) 
{        
    zmq::message_t update;
    subscriber.recv(&update);
    i++;
    std::cout<<"receiving  :"<<i<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

发布商代码(在订阅者之后启动)

zmq::context_t context (1);
zmq::socket_t publisher (context, ZMQ_PUB);
publisher.bind("tcp://*:5556");

int i = 0;
for (int update_nbr = 0; update_nbr < 100; update_nbr++) 
{        
    //  Send message to all subscribers
    zmq::message_t request (20);

    time_t seconds;
    seconds = time (NULL);

    char update [20]="";
    sprintf (update, "%ld", seconds);

    memcpy …
Run Code Online (Sandbox Code Playgroud)

c++ publish-subscribe zeromq

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

Python Sqlite3获取Sqlite连接路径

给定一个sqlite3连接对象,如何检索sqlite3文件的文件路径?

python sqlite

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

在VBA中检查哪个Excel工作表处于活动状态(当前显示)

我正在寻找一种方法来检查Excel工作表当前是否处于活动状态(当前显示).我对同步方法感兴趣,而不是在事件中.

excel vba excel-vba

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

什么是避免在字符串中指定相同值的最pythonic方法

message = "hello %s , how are you %s, welcome %s"%("john","john","john")
Run Code Online (Sandbox Code Playgroud)

什么是避免指定"john"3次而是指定一个短语的最pythonic方法.

python string formatting

8
推荐指数
2
解决办法
286
查看次数

Python - excel:写入多个单元格需要时间

我正在使用 win32com.client 将数据写入 Excel 文件。这花费了太多时间(下面的代码模拟了我想要更新 Excel 的数据量,大约需要 2 秒)。

有没有一种方法可以在一次调用中更新多个单元格(具有不同的值),而不是一个一个地填充它们?或者也许使用更有效的不同方法?

我正在使用 python 2.7 和 Office 2010。

这是代码:

from win32com.client import Dispatch

xlsApp   = Dispatch('Excel.Application')
xlsApp.Workbooks.Add()
xlsApp.Visible  = True
workSheet = xlsApp.Worksheets(1)

for i in range(300):
    for j in range(20):
        workSheet.Cells(i+1,j+1).Value = (i+10000)*j
Run Code Online (Sandbox Code Playgroud)

python excel

5
推荐指数
1
解决办法
4394
查看次数

python请求非ascii文件名的问题

我正在使用python请求发布请求.当附件参数有一些非ascii字符时会引发异常,在其他只存在ascii数据的情况下,一切都很好.

你可以在这里看到例外

response = requests.post(url="https://api.mailgun.net/v2/%s/messages" % utils.config.mailDomain,
                auth=("api", utils.config.mailApiKey),
                data={
                        "from" : me,
                        "to" : recepients,
                        "subject" : subject,
                        "html" if html else "text" : message
                    },

                files= [('attachment', codecs.open(f.decode('utf8'))) for f in attachments] if attachments and len(attachments) else []                                
                )
Run Code Online (Sandbox Code Playgroud)

编辑: 使用utf8解码文件名后,我没有得到异常,但文件没有附加.我通过在其名称中附加仅包含ascii字符的文件来调试请求,并且请求标头请求构建是:

{'Content-Type': None, 'Content-Location': None, 'Content-Disposition': u'form-data; name="attachment"; filename="Hello.docx"'}
Run Code Online (Sandbox Code Playgroud)

这成功了,我收到附件的邮件.

但是,当使用带有希伯来字符的文件时,请求的标题是:

{'Content-Type': None, 'Content-Location': None, 'Content-Disposition': 'form-data; name="attachment"; filename*=utf-8\'\'%D7%91%D7%93%D7%99%D7%A7%D7%94.doc'}
Run Code Online (Sandbox Code Playgroud)

我收到邮件但没有附加文件.有任何想法吗?

python encoding decode python-requests mailgun

5
推荐指数
1
解决办法
1993
查看次数

Python Makepy 与 Office 2013 (office 15)

我在 office 2010 中使用 python 和 excel,在那里没有问题。我使用 python 的 makepy 模块来绑定到 txcel com 对象。

但是,在另一台计算机上,我安装了 office 2013,当我启动 makepy 时,没有列出 excel 选项(与 office 2010 不同,其中 makepy 列出了“Microsoft Excel 14.0 对象库”)。

我在注册表中搜索了“Microsoft Excel 15.0 对象库”,它就在那里。我尝试使用 :makepy -d 'Microsoft Excel 15.0 Object Library' 但这没有用。

帮助将不胜感激。谢谢。

python excel win32com office-2013

4
推荐指数
1
解决办法
2170
查看次数

当孩子长大后,如何让html div扩展?

我有一个主要div包含一个表:

<div id="main_container"><table>
Run Code Online (Sandbox Code Playgroud)

你可以在http://jsfiddle.net/tF62u/看到完整的代码

如您所见,子表比div宽,但它(div)不会相应调整.

我在这里错过了哪个定义?

html css position

3
推荐指数
1
解决办法
139
查看次数

Jquery动态按钮:如何按类绑定到exisitng click事件

我有一个由jquery ready范围内的类选择器触发的click事件:

    $(".testBtn").click(function()
    {
        alert("This Works");      
    });
Run Code Online (Sandbox Code Playgroud)

这适用于静态按钮,但这对于动态按钮不起作用,动态按钮在单击带有"addRowBtn"id的按钮时添加.我猜测它与事件被注册后创建按钮有关,但是新按钮仍然有'testBtn'类,所以它应该有效.知道我做错了什么以及如何将动态按钮注册到类选择器点击事件?

这是整个代码,您可以复制并粘贴到html中,单击"添加按钮"并尝试单击添加的按钮.你会发现什么都没发生.

<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js'></script>
<script>
$(function()
{    
    $(".addRowBtn").click(function()
    {
        $("#mainTable tr:last").after("<tr><td><button class='testBtn'>NotWorking</button></td></tr>");           
    });

    $(".testBtn").click(function()
    {
        alert("This Works");      
    });
});
</script>
</head>
<body>

<table id="mainTable">
<button class="addRowBtn">Add button</button>
<tr><th>Testing</th></tr>
<tr><td><button class='testBtn'>Working</button></td></tr>
</table>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

jquery

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

用于输入字段的Ajax代码,用于表示Gmail的字段

我有兴趣将输入字段编码为gmail的撰写屏幕中的"to"字段.这意味着每个数据都使用一个框架封装,该框架具有"x"选项以将其删除.

gmail到现场

javascript ajax

2
推荐指数
1
解决办法
726
查看次数

如何根据ng-href中的数据返回动态html?

这是我的流程:主页面位于'/ main'网址,因此当用户访问该页面时,我的Web服务器返回一个静态html:

<!DOCTYPE html>
<html ng-app="sam">
    <head>
        <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    </head>
    <body>
        <div ng-controller="BuildingsController as buildings">  
            <button ng-click="RefreshBuildings()">Refresh</button>
            <h1>{{buildings.elements.length}}</h1>
            <table>
                <tr>
                <th>id</th>
                <th>name</th>
                <th>last_updated</th>
                <th>based_on_file</th>
                </tr>
                <tr ng-repeat="element in buildings.elements | orderBy:'-id'">              
                    <td>{{element.id}}</td>
                    <td><a ng-href="/getTenants?buildingId={{element.id}}">{{element.name}}</a></td>
                    <td>{{element.last_updated | date}}</td>
                    <td>{{element.based_on_file}}</td>
                    <td><button ng-click="buildings.UpdateDataBase(element.id)">Refresh</button></td>
                </tr>
            </table>
        </div>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.13/angular.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript" src="app.js"></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是js文件:

(function(){
    var app = angular.module('sam', []);
    app.controller('BuildingsController', ['$http','$log', function($http, $log){

        //Update scope pramaters
        this.RefreshGui = function(elements){
            this.elements = elements
        };

        //call server to …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery angularjs

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

C++无法填充C数组的映射

typedef std::map<std::string,int> string2intMap;
typedef string2intMap arrOfMaps[3] ;

//map : string --> array of maps of <std::string,int>
std::map<std::string,arrOfMaps> tryingStuff;

//map : string --> int
string2intMap s;
s["key"]= 100;

tryingStuff["hello"][0] = s;
Run Code Online (Sandbox Code Playgroud)

上面的代码没有编译,有问题的行是:tryingStuff ["hello"] [0] = s;

这是编译器大喊:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(215): error C2440: '<function-style-cast>' : cannot convert from 'int' to 'std::map<_Kty,_Ty> [3]'
2>          with
2>          [
2>              _Kty=std::string,
2>              _Ty=int
2>          ]
2>          There are no conversions to array types, although there are conversions to references or pointers to …
Run Code Online (Sandbox Code Playgroud)

c++ arrays typedef visual-c++

0
推荐指数
1
解决办法
176
查看次数