Mimic curl in python

前提是你 提交于 2020-05-15 04:52:36

问题


The following curl code works:

curl --form addressFile=@t.csv --form benchmark=Public_AR_Census2010 http://geocoding.geo.census.gov/geocoder/locations/addressbatch

t.csv is simply

1, 800 Wilshire Blvd, Los Angeles, CA, 90017

How do I mimic this in python. So far all of my attempts have resulted in 'Bad Requests'. I am also trying to keep everything in memory--no writing to file.

One attempt:

import requests
url = "http://geocoding.geo.census.gov/geocoder/json/addressbatch"

# data is csv like string with , and \n
ddata = urllib.urlencode({'addressFile' : data, 'benchmark' : 'Public_AR_Current'})
r = requests.get(url + "?" + ddata) # Forcibly closed by remote

requests.put("http://geocoding.geo.census.gov/geocoder/json/addressbatch", ddata)

回答1:


One option would be to use requests:

import requests

url = "http://geocoding.geo.census.gov/geocoder/locations/addressbatch"
data = {'benchmark': 'Public_AR_Census2010'}
files = {'addressFile': open('t.csv')}

response = requests.post(url, data=data, files=files)
print response.content

prints:

"1"," 800 Wilshire Blvd,  Los Angeles,  CA,  90017","Match","Exact","800 Wilshire Blvd, LOS ANGELES, CA, 90017","-118.25818,34.049366","141617176","L"

In case you need to handle the csv data in memory, initialize a StringIO buffer:

from StringIO import StringIO
import requests

csv_data = "1, 800 Wilshire Blvd, Los Angeles, CA, 90017"
buffer = StringIO()
buffer.write(csv_data)
buffer.seek(0)

url = "http://geocoding.geo.census.gov/geocoder/locations/addressbatch"
data = {'benchmark': 'Public_AR_Census2010'}
files = {'addressFile': buffer}

response = requests.post(url, data=data, files=files)
print response.content

This prints the same result as is with using a real file.



来源:https://stackoverflow.com/questions/25024087/mimic-curl-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!