有没有办法在Python中生成随机字母(如random.randint但字母)?random.randint的范围功能会很好但是有一个只输出随机字母的生成器会比什么都好.
我正在执行一个使用Python请求库上传文件的简单任务.我搜索了Stack Overflow,似乎没有人遇到同样的问题,即服务器没有收到该文件:
import requests
url='http://nesssi.cacr.caltech.edu/cgi-bin/getmulticonedb_release2.cgi/post'
files={'files': open('file.txt','rb')}
values={'upload_file' : 'file.txt' , 'DB':'photcat' , 'OUT':'csv' , 'SHORT':'short'}
r=requests.post(url,files=files,data=values)
Run Code Online (Sandbox Code Playgroud)
我正在用我的文件名填充'upload_file'关键字的值,因为如果我把它留空,它会说
Error - You must select a file to upload!
Run Code Online (Sandbox Code Playgroud)
现在我明白了
File file.txt of size bytes is uploaded successfully!
Query service results: There were 0 lines.
Run Code Online (Sandbox Code Playgroud)
仅当文件为空时才会出现.所以我不知道如何成功发送我的文件.我知道该文件有效,因为如果我去这个网站并手动填写表格,它会返回一个很好的匹配对象列表,这就是我所追求的.我非常感谢所有提示.
其他一些线程相关(但没有回答我的问题):
我正在编写一些与redmine接口的代码,我需要上传一些文件作为过程的一部分,但我不知道如何从包含二进制文件的python中执行POST请求.
我试图在这里模仿命令:
curl --data-binary "@image.png" -H "Content-Type: application/octet-stream" -X POST -u login:password http://redmine/uploads.xml
Run Code Online (Sandbox Code Playgroud)
在python(下面)中,它似乎不起作用.我不确定问题是否与编码文件有关,或者标题是否有问题.
import urllib2, os
FilePath = "C:\somefolder\somefile.7z"
FileData = open(FilePath, "rb")
length = os.path.getsize(FilePath)
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, 'http://redmine/', 'admin', 'admin')
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
request = urllib2.Request( r'http://redmine/uploads.xml', FileData)
request.add_header('Content-Length', '%d' % length)
request.add_header('Content-Type', 'application/octet-stream')
try:
response = urllib2.urlopen( request)
print response.read()
except urllib2.HTTPError as e:
error_message = e.read()
print error_message
Run Code Online (Sandbox Code Playgroud)
我有权访问服务器,它看起来像编码错误:
...
invalid byte sequence in UTF-8
Line: 1
Position: …Run Code Online (Sandbox Code Playgroud) 我正在编写一个脚本,使用RFC 2388中multipart/form-data定义的内容类型上传包含文件的内容.从长远来看,我正在尝试提供一个简单的Python脚本来为github上传二进制包,这涉及将类似表格的数据发送到Amazon S3.
这个问题已经问过如何做到这一点,但到目前为止它还没有得到公认的答案,而且目前这两个答案中更有用的是指出这些方法,这些方法反过来手动构建整个消息.我有点担心这种方法,特别是关于字符集和二进制内容.
还有这个问题,其目前得分最高的答案暗示了该MultipartPostHandler模块.但这与我提到的食谱没什么不同,因此我的担忧也适用于那些.
RFC 2388第4.3节明确规定除非另有声明,否则内容应为7位,因此可能需要Content-Transfer-Encoding标头.这是否意味着我必须对Base64编码二进制文件内容?或者Content-Transfer-Encoding: 8bit对于任意文件是否足够?或者应该读一下Content-Transfer-Encoding: binary?
一般的filename标题字段,特别是标题字段,默认情况下仅为ASCII.我希望我的方法能够传递非ASCII文件名.我知道,对于我目前为github上传内容的应用程序,我可能不需要它,因为文件名是在一个单独的字段中给出的.但我希望我的代码可以重用,所以我宁愿以一致的方式编码文件名参数.RFC 2388第4.4节建议RFC 2231中引入的格式,例如filename*=utf-8''t%C3%A4st.txt.
由于multipart/form-data本质上是一种MIME类型,我认为它应该是可以使用的email包从标准Python库撰写我的职务.特别是非ASCII头字段的相当复杂的处理是我想委托的.
所以我写了下面的代码:
#!/usr/bin/python3.2
import email.charset
import email.generator
import email.header
import email.mime.application
import email.mime.multipart
import email.mime.text
import io
import sys
class …Run Code Online (Sandbox Code Playgroud) 这是使用Python脚本中的POST发送文件的几乎重复,但我想添加一个警告:我需要一些能够正确处理字段和附加文件编码的内容.当你将包含非ascii字符的unicode字符串放入混合中时,我已经能够找到解决方案.此外,大多数解决方案不会对数据进行64位编码,以保持7位清洁.
我有兴趣编写一个简短的python脚本,它通过POST请求将一个简短的二进制文件(.wav/.raw音频)上传到远程服务器.
我用pycurl完成了这个,这使得它非常简单并且产生了一个简洁的脚本; 不幸的是,它还要求最终用户安装了pycurl,我不能依赖它.
我在其他帖子中也看到了一些仅依赖于基本库,urllib,urllib2等的例子,但这些通常看起来相当冗长,这也是我想要避免的.
我想知道是否有任何简洁的例子不需要使用外部库,并且第三方可以快速方便地理解 - 即使他们不是特别熟悉python.
我目前使用的是什么样的,
def upload_wav( wavfile, url=None, **kwargs ):
"""Upload a wav file to the server, return the response."""
class responseCallback:
"""Store the server response."""
def __init__(self):
self.contents=''
def body_callback(self, buf):
self.contents = self.contents + buf
def decode( self ):
self.contents = urllib.unquote(self.contents)
try:
self.contents = simplejson.loads(self.contents)
except:
return self.contents
t = responseCallback()
c = pycurl.Curl()
c.setopt(c.POST,1)
c.setopt(c.WRITEFUNCTION, t.body_callback)
c.setopt(c.URL,url)
postdict = [
('userfile',(c.FORM_FILE,wavfile)), #wav file to post
]
#If there are extra keyword args …Run Code Online (Sandbox Code Playgroud) 使用CURL,我可以发布一个类似的文件
CURL -X POST -d "pxeconfig=`cat boot.txt`" https://ip:8443/tftp/syslinux
Run Code Online (Sandbox Code Playgroud)
我的档案看起来像
$ cat boot.txt
line 1
line 2
line 3
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用python中的请求模块来实现相同的目的
r=requests.post(url, files={'pxeconfig': open('boot.txt','rb')})
Run Code Online (Sandbox Code Playgroud)
当我在服务器端打开文件时,该文件包含
{:filename=>"boot.txt", :type=>nil, :name=>"pxeconfig",
:tempfile=>#<Tempfile:/tmp/RackMultipart20170405-19742-1cylrpm.txt>,
:head=>"Content-Disposition: form-data; name=\"pxeconfig\";
filename=\"boot.txt\"\r\n"}
Run Code Online (Sandbox Code Playgroud)
请提出如何实现这一目标。
嗨,我在使用Python请求库(http://docs.python-requests.org/en/latest/index.html)发布文本文件时遇到问题,你能让我知道我做错了什么吗?
我尝试搜索相关问题,并从Python脚本中使用POST发现此发送文件,但它没有回答我的问题.
这是我的代码:
import codecs
import requests
# Create a text file
savedTextFile = codecs.open('mytextfile.txt', 'w', 'UTF-8')
# Add some text to it
savedTextFile.write("line one text for example\n and line two text for example")
# Post the file THIS IS WHERE I GET REALLY TRIPPED UP
myPostRequest = requests.post("https://someURL.com", files=savedTextFile)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了上面的一些变化,我没有得到任何地方(每次都有新的错误).如何发布我刚刚创建的这个txt文件?我试图发布的API需要将文本文件发布到它.
任何帮助表示赞赏!
我正在尝试将文件发送到 API,然后获取响应 - 一个 CSV 文件(我看过有关它的不同 帖子,但我无法使其工作)
文档中的示例使用 httpie
http --timeout 600 -f POST http://api-adresse.data.gouv.fr/search/csv/ data@path/to/file.csv
Run Code Online (Sandbox Code Playgroud)
但是当我使用请求时,我得到一个 400 Bad Request
path = '/myfile.csv'
url = 'http://api-adresse.data.gouv.fr/search/csv/'
files = {'file': open(path, 'rb')}
res = requests.post(url, data=files)
Run Code Online (Sandbox Code Playgroud) python ×9
post ×4
http ×2
python-3.x ×2
amazon-s3 ×1
api ×1
binary ×1
boundary ×1
encoding ×1
file ×1
file-upload ×1
http-post ×1
http-request ×1
mime ×1
python-2.7 ×1
random ×1
redmine ×1
rest ×1
urllib2 ×1