关于Python文件格式的新手问题

发布于 2024-10-11 04:12:15 字数 923 浏览 3 评论 0原文

我正在 Python 2.7 中使用 pycURL 库编写一个简单的程序,将文件内容提交到 Pastebin。 下面是程序的代码:

#!/usr/bin/env python2

import pycurl, os

def send(file):
    print "Sending file to pastebin...."
    curl = pycurl.Curl()
    curl.setopt(pycurl.URL, "http://pastebin.com/api_public.php")
    curl.setopt(pycurl.POST, True)
    curl.setopt(pycurl.POSTFIELDS, "paste_code=%s" % file)
    curl.setopt(pycurl.NOPROGRESS, True)
    curl.perform()

def main():
    content = raw_input("Provide the FULL path to the file: ")
    open = file(content, 'r')
    send(open.readlines())
    return 0

main()

输出的 Pastebin 看起来像标准的 Python 列表: ['string\n', 'line of text\n', ...] 等。

有什么办法可以格式化它,使其看起来更好并且实际上是人类可读的?另外,如果有人能告诉我如何在 POSTFIELDS 中使用多个数据输入,我将非常高兴。 Pastebin API 使用 paste_code 作为其主要数据输入,但它可以使用可选的内容,例如设置上传名称的 paste_name 或设置上传名称的 paste_private它是私人的。

I'm writing a simple program in Python 2.7 using pycURL library to submit file contents to pastebin.
Here's the code of the program:

#!/usr/bin/env python2

import pycurl, os

def send(file):
    print "Sending file to pastebin...."
    curl = pycurl.Curl()
    curl.setopt(pycurl.URL, "http://pastebin.com/api_public.php")
    curl.setopt(pycurl.POST, True)
    curl.setopt(pycurl.POSTFIELDS, "paste_code=%s" % file)
    curl.setopt(pycurl.NOPROGRESS, True)
    curl.perform()

def main():
    content = raw_input("Provide the FULL path to the file: ")
    open = file(content, 'r')
    send(open.readlines())
    return 0

main()

The output pastebin looks like standard Python list: ['string\n', 'line of text\n', ...] etc.

Is there any way I could format it so it looks better and it's actually human-readable? Also, I would be very happy if someone could tell me how to use multiple data inputs in POSTFIELDS. Pastebin API uses paste_code as its main data input, but it can use optional things like paste_name that sets the name of the upload or paste_private that sets it private.

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

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

发布评论

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

评论(4

海夕 2024-10-18 04:12:15

首先,按照 virhilo 所说使用 .read() 。

另一步是使用 urllib.urlencode() 获取字符串:

curl.setopt(pycurl.POSTFIELDS, urllib.urlencode({"paste_code": file}))

这还允许您发布更多字段:

curl.setopt(pycurl.POSTFIELDS, urllib.urlencode({"paste_code": file, "paste_name": name}))

First, use .read() as virhilo said.

The other step is to use urllib.urlencode() to get a string:

curl.setopt(pycurl.POSTFIELDS, urllib.urlencode({"paste_code": file}))

This will also allow you to post more fields:

curl.setopt(pycurl.POSTFIELDS, urllib.urlencode({"paste_code": file, "paste_name": name}))
生死何惧 2024-10-18 04:12:15
import pycurl, os

def send(file_contents, name):
    print "Sending file to pastebin...."
    curl = pycurl.Curl()
    curl.setopt(pycurl.URL, "http://pastebin.com/api_public.php")
    curl.setopt(pycurl.POST, True)
    curl.setopt(pycurl.POSTFIELDS, "paste_code=%s&paste_name=%s" \
                                   % (file_contents, name))
    curl.setopt(pycurl.NOPROGRESS, True)
    curl.perform()


if __name__ == "__main__":
    content = raw_input("Provide the FULL path to the file: ")
    with open(content, 'r') as f:
        send(f.read(), "yournamehere")
    print

读取文件时,使用 with 语句(这可以确保在出现问题时正确关闭文件)。

不需要有一个 main 函数然后调用它。使用 if __name__ == "__main__" 构造让脚本在调用时自动运行(除非将其作为模块导入)。

要发布多个值,您可以手动构建 url:只需用与号 (&) 分隔不同的键、值对。像这样:key1=value1&key2=value2。或者您可以使用 urllib.urlencode 构建一个(正如其他人建议的那样)。

编辑:在要发布的字符串上使用urllib.urlencode可以确保当源字符串包含一些有趣/保留/异常字符时内容正确编码。

import pycurl, os

def send(file_contents, name):
    print "Sending file to pastebin...."
    curl = pycurl.Curl()
    curl.setopt(pycurl.URL, "http://pastebin.com/api_public.php")
    curl.setopt(pycurl.POST, True)
    curl.setopt(pycurl.POSTFIELDS, "paste_code=%s&paste_name=%s" \
                                   % (file_contents, name))
    curl.setopt(pycurl.NOPROGRESS, True)
    curl.perform()


if __name__ == "__main__":
    content = raw_input("Provide the FULL path to the file: ")
    with open(content, 'r') as f:
        send(f.read(), "yournamehere")
    print

When reading files, use the with statement (this makes sure your file gets closed properly if something goes wrong).

There's no need to be having a main function and then calling it. Use the if __name__ == "__main__" construct to have your script run automagically when called (unless when importing this as a module).

For posting multiple values, you can manually build the url: just seperate different key, value pairs with an ampersand (&). Like this: key1=value1&key2=value2. Or you can build one with urllib.urlencode (as others suggested).

EDIT: using urllib.urlencode on strings which are to be posted makes sure content is encoded properly when your source string contains some funny / reserved / unusual characters.

风尘浪孓 2024-10-18 04:12:15

使用 .read() 而不是 .readlines()

use .read() instead of .readlines()

梓梦 2024-10-18 04:12:15

POSTFIELDS 的发送方式应与发送查询字符串参数的方式相同。因此,首先,有必要对您想要的字符串进行编码发送到 paste_code,然后使用 & 您可以添加更多 POST 参数。

示例:

paste_code=hello%20world&paste_name=test

祝你好运!

The POSTFIELDS should be sended the same way as you send Query String arguments. So, in the first place, it's necessary to encode the string that you're sending to paste_code, and then, using & you could add more POST arguments.

Example:

paste_code=hello%20world&paste_name=test

Good luck!

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