1

I can't use the python requests package (long story, let's just assume I've exhausted all possibilities for using that package)

Is there an alternative package that would provide the exact functionality of the following code?

import requests
requests.post(URL, data=DATA, auth=(USERNAME, PASSWORD), headers=HEADER)
Craig
  • 835
  • 1
  • 7
  • 21
  • Not being able to use it means you can't install it? – Josep Valls Dec 15 '15 at 23:32
  • Even if you can't install it, requests is a pure python module and you can just download it and unzip it into your application's directory and import it from there. Otherwise, look at the code and copy it out! – Josep Valls Dec 15 '15 at 23:33
  • 2
    Nah, it's installed. But this is for a big django page and, unfortunately, one of the apps was quite foolishly named requests, so import requests imports that app. I've tried referencing the file path of requests directly without much luck. Maybe I'll keep trying that -- was looking for another way. – Craig Dec 15 '15 at 23:56
  • In our case it is a legal issue that requests library has dependency of chardet which is License: GNU Library or Lesser General Public License (LGPL) (LGPL) – vogash Mar 19 '19 at 08:34

4 Answers4

4

One of alternative ways, httplib usage

import httplib
import urllib
from base64 import b64encode

# your form
form_data = {'a':1 'b':2}
params = urllib.urlencode(form_data)

# build authorization
user_and_pass = b64encode(b"username:password").decode("ascii")

# headers
headers = {'Authorization': 'Basic %s' % user_and_pass}

# connection
conn = httplib.HTTPConnection("example.com")
conn.request('POST', '/v3/call_api', params, headers)

# get result
response = conn.getresponse()

print response.status, response.reason

data = response.read()

conn.close()
mrDinkelman
  • 488
  • 1
  • 9
  • 18
3

If your only problem is importing because of the module name, you can always import by full path or alter the module search path and reset it after importing. In order to avoid conflicts you can use import requests as requests2 or something of the like. See the following question for the first option or the documentation about the search path.

How to import a module given the full path?

https://docs.python.org/2/tutorial/modules.html#the-module-search-path

Community
  • 1
  • 1
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • I totally agree this is normally the correct route; unfortunately, I spent about an hour trying this before posting my question without any luck. Our server setup is a bit funky right now, which is complicating things. – Craig Dec 16 '15 at 12:58
1

from urllib2.urlopen

"the HTTP request will be a POST instead of a GET when the data parameter is provided."

So something like (i'll use an image example just because it was so darn obscure to figure out):

import urllib
import urllib2

import numpy as np
import cv2

image_to_send = np.zeros(512, np.uint8)
buffer_image = \
    np.array(cv2.imencode('.png', image_to_send)[1]).tostring()
post_data = \
    urllib.urlencode((('img_type','.png'),('img_data',buffer_image)))
req = \
    urllib2.urlopen('http://www.yourdestination.com/imageupload/', data=post_data)

if you want authentication, you'll need to subclass urllib.FancyURLopener, and override prompt_user_passwd

0

There is an alternative to python requests you can try, https://github.com/juancarlospaco/faster-than-requests

Murad
  • 1,064
  • 16
  • 13