11

I'm pretty new to Python and I've been using their requests module as a substitute for PHP's cURL library. My code is as follows

import requests
import json
import os
import urllib
import math
import sys
def main() : 
 url = 'https://api.com'
 headers = {'Content-Type': 'application/json; charset=utf-8',
 'User-Agent': '(iPhone; iOS 7.0.4; Scale/2.00)'}
 d = {'token': "12345"}
 proxies = {
 "https": "https://27.254.52.99:8080",
 }
 post = json.dumps(d);
 r = requests.post(url, data=post, headers=headers, proxies=proxies)
 print r.json
if __name__ == "__main__":
 main()

However, I'm greeted with the following error:

File "test.py", line 42, in test
r = requests.post(url, data=post, headers=headers, proxies=proxies)
File "/Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg/requests/api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "/Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg/requests/sessions.py", line 383, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg/requests/sessions.py", line 486, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg/requests/adapters.py", line 381, in send
raise ProxyError(e)
ProxyError: Cannot connect to proxy. Socket error: [Errno 54] Connection reset by peer.
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Jun 5, 2014 at 10:46
2
  • Can't find anything odd about the code. And the error seems to indicate there is a connection issue. Does curl or wget work from the same machine? Commented Jun 5, 2014 at 11:03
  • Here is also a nice tool to play with proxies. charlesproxy.com Commented Jun 5, 2014 at 11:07

1 Answer 1

12

Edit June 2019: This reply is not relevant anymore. Issue(s) are fixed.

Edit 2: "note that even for https proxy, the proxy address' scheme is http, it's because the client and proxy server initiate the tunnelling(the CONNECT method) in plain http. However, that's may not be true 3 years ago." - From the comments

HTTPS is 'bugged' in requests. I don't know the specifics but you can find a few other topics on this website concerning the issue. Also a Github issue is still active here. I'm suspecting you're having the problems mentioned there. If I am totally wrong, someone correct me.

To verify:

$~ curl --proxy https://27.254.52.99:8080 icanhazip.com
27.254.52.99

Works, but then in Python:

>>> proxies={'https': 'https://27.254.52.99:8080'}
>>> r = requests.get('http://icanhazip.com', headers={'User-Agent': 'Bla'}, proxies=proxies)
print r.content
<my ipv6 address comes up>

As you can see, my address comes up which means the proxy did nothing.

I don't understand why you are receiving a stacktrace. Maybe because your API is on HTTPS as well (?). Or maybe your API is just... down.

Anyway, the proxy does work in requests if its over HTTP.

>>> proxies={'http': 'http://27.254.52.99:8080'}
>>> r = requests.head('http://icanhazip.com', headers={'User-Agent': 'Bla'}, proxies=proxies)
print r.content
27.254.52.99
answered Jun 5, 2014 at 11:06
Sign up to request clarification or add additional context in comments.

8 Comments

@Lance: Any luck yet?
Sorry for he delay. Yes, this worked for me. Thank you.
You guys all misunderstood what a https proxy is. the correct form should be {'https': 'example.com:3128'}, note the http part
@ospider how is that different from my 1st example? besides, this answer was based on a bug requests was having, 3 years ago.
note that even for https proxy, the proxy address' scheme is http, it's because the client and proxy server initiate the tunnelling(the CONNECT method) in plain http. However, that's may not be true 3 years ago.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.