关于Python文件格式的新手问题
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,按照 virhilo 所说使用 .read() 。
另一步是使用 urllib.urlencode() 获取字符串:
这还允许您发布更多字段:
First, use
.read()
asvirhilo
said.The other step is to use
urllib.urlencode()
to get a string:This will also allow you to post more fields:
读取文件时,使用
with
语句(这可以确保在出现问题时正确关闭文件)。不需要有一个 main 函数然后调用它。使用
if __name__ == "__main__"
构造让脚本在调用时自动运行(除非将其作为模块导入)。要发布多个值,您可以手动构建 url:只需用与号 (
&
) 分隔不同的键、值对。像这样:key1=value1&key2=value2
。或者您可以使用 urllib.urlencode 构建一个(正如其他人建议的那样)。编辑:在要发布的字符串上使用
urllib.urlencode
可以确保当源字符串包含一些有趣/保留/异常字符时内容正确编码。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 theif __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 withurllib.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.使用 .read() 而不是 .readlines()
use .read() instead of .readlines()
POSTFIELDS
的发送方式应与发送查询字符串参数的方式相同。因此,首先,有必要对您想要的字符串进行编码发送到paste_code
,然后使用&
您可以添加更多 POST 参数。示例:
祝你好运!
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 topaste_code
, and then, using&
you could add more POST arguments.Example:
Good luck!