Python:将大字典发送到服务器

发布于 2024-07-21 23:40:06 字数 413 浏览 11 评论 0原文

我有一个应用程序应该将状态信息传递给服务器。 该信息实际上是一个带有字符串键的大字典。

服务器将运行基于 Turbogears 的 Web 应用程序,因此调用的服务器端方法接受任意数量的关键字参数。

除了实际数据之外,还应该传输一些与身份验证相关的数据(id、密码..)。 一种方法是简单地对包含所有这些的大字典进行 urlencode 并将其在请求中发送到服务器。

urllib2.urlencode(dataPlusId)

但实际上,进行身份验证和接受数据集的方法不必对数据了解太多。 数据可以透明地传输和接受,并移交给另一种处理数据的方法。

所以我的问题是:将大型数据字典传输到服务器的最佳方法是什么? 并且,在这种特定情况下,处理身份验证的最佳方法是什么?

I have an application that should communicate status information to a server. This information is effectively a large dictionary with string keys.

The server will run a web application based on Turbogears, so the server-side method called accepts an arbitrary number of keyword arguments.

In addition to the actual data, some data related to authentication (id, password..) should be transmitted. One approach would be to simply urlencode a large dictionary containing all this and send it in a request to the server.

urllib2.urlencode(dataPlusId)

But actually, the method doing the authentication and accepting the data set does not have to know much about the data. The data could be transmitted and accepted transparently and handed over to another method working with the data.

So my question is: What is the best way to transmit a large dictionary of data to a server in general? And, in this specific case, what is the best way to deal with authentication here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

玩套路吗 2024-07-28 23:40:06

我认为最好的方法是以适当的传输格式对数据进行编码(您不应该使用 pickle,因为它不会保存,但它可以是二进制的)并将其传输为 多部分帖子请求

我不知道你是否可以让它与 repoze.who 一起工作。 如果它不支持一步登录和函数调用,您可能需要自己验证凭据。

如果您可以将数据包装在 xml 中,您还可以使用 XML-RPC

I think the best way is to encode your data in an appropriate transfer format (you should not use pickle, as it's not save, but it can be binary) and transfer it as a multipart post request

What I do not know if you can make it work with repoze.who. If it does not support sign in and function call in one step, you'll perhaps have to verify the credentials yourself.

If you can wrap your data in xml you could also use XML-RPC.

初见 2024-07-28 23:40:06

如果安全是一个问题,我同意所有关于避免 pickle 的答案(如果发送者在数据 unpickle 之前得到身份验证,则可能不是这样 - 但是,当安全性出现问题时,两级防御可能比一级更好); 在这种情况下,JSON 通常会很有帮助(或者,XML,如果没有其他办法的话......!-)。

正如 SpliFF 建议的那样,身份验证最好留给网络服务器,而 SSL(即 HTTPS)通常对此很有帮助。 如果这是不可行的,但让客户端和服务器共享“秘密”是可行的,那么以加密形式发送序列化字符串可能是最好的。

I agree with all the answers about avoiding pickle, if safety is a concern (it might not be if the sender gets authenticated before the data's unpickled -- but, when security's at issue, two levels of defense may be better than one); JSON is often of help in such cases (or, XML, if nothing else will do...!-).

Authentication should ideally be left to the webserver, as SpliFF recommends, and SSL (i.e. HTTPS) is generally good for that. If that's unfeasible, but it's feasible to let client and server share a "secret", then sending the serialized string in encrypted form may be best.

终止放荡 2024-07-28 23:40:06

为什么不将字典序列化到文件中,然后上传该文件? 这样,服务器就可以将对象读回到字典中。

Why don't you serialize the dictionary to a file, and upload the file? This way, the server can read the object back into a dictionary .

神经暖 2024-07-28 23:40:06

对 python 数据进行 POST(按照其他答案中的建议使用二进制文件)并使用网络服务器处理安全性。 Apache 和 Microsoft 服务器都可以使用多种方法(SSL 客户端证书、密码、系统帐户等)进行身份验证。

如果您只想将其返回到文本或 XML,则序列化/反序列化到文本或 XML 可能有点过分了再次字典)。

Do a POST of your python data (use binary as suggested in other answers) and handle security using your webserver. Apache and Microsoft servers can both do authentication using a wide variety of methods (SSL client certs, Password, System accounts, etc...)

Serialising/Deserialising to text or XML is probably overkill if you're just going to turn it back to dictionary again).

眼藏柔 2024-07-28 23:40:06

您是否尝试过在数据上使用 pickle ?

Have you tried using pickle on the data ?

只为守护你 2024-07-28 23:40:06

我个人会在两端使用 SimpleJSON,然后将“文件”(它实际上只是一个流)作为多部分数据发布。

但这就是我。 还有其他选择。

I'd personally use SimpleJSON at both ends and just post the "file" (it would really just be a stream) over as multipart data.

But that's me. There are other options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文