top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Proxy connection with Python

0 votes
241 views

I have an issue that has been frustrating me for a while now.

This is an update of a crosspost
(http://stackoverflow.com/questions/16703936/proxy-connection-with-python)
which I made over a month ago.

I have been attempting to connect to URLs from python. I have tried: urllib2, urlib3, and requests. It is the same issue that i run up against in all cases. Once I get the answer I imagine all three of them would work fine.

The issue is connecting via proxy. I have entered our proxy information but am not getting any joy. I am getting 407 codes and error messages like: HTTP Error 407: Proxy Authentication Required (Forefront TMG requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )

I think that this also stops me using pip to install (at least from remotes ). I get 'Cannot fetch index base URL http://pypi.python.org/simple/". I end up using git to clone a local copy of the repo and install from that.

However, I can connect using a number of other applications that go through the proxy, git and pycharm for example. When I run git config -- get htpp.proxy it returns the same values and format that I am entering in Python namely:

http://username:password@proxy:8080

An example of code in requests is

import requests
proxy = {"http": "http://username:password@proxy:8080"}
url = 'http://example.org'
r = requests.get(url, proxies=proxy)
print r.status_code
posted Jun 26, 2013 by anonymous

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
0 votes

I am trying to use mitmproxy behind a company proxy that requires a user/password login.

The setup is: Local PC's browser -> mitmproxy (on local PC) -> company proxy -> internet.

Based on this SO thread, this is how you use mitmproxy within a Python program. This example works fine when there's no proxy.

from mitmproxy.options import Options
from mitmproxy.proxy.config import ProxyConfig
from mitmproxy.proxy.server import ProxyServer
from mitmproxy.tools.dump import DumpMaster

class Addon(object):
    def __init__(self):
        pass

    def request(self, flow):
        # examine request here 
        pass

    def response(self, flow):
        # examine response here
        pass


if __name__ == "__main__":

    options = Options(listen_host='0.0.0.0', listen_port=8080, http2=True)
    m = DumpMaster(options, with_termlog=False, with_dumper=False)
    config = ProxyConfig(options)

    m.server = ProxyServer(config)
    m.addons.add(Addon())

    try:
        print('starting mitmproxy')
        m.run()
    except KeyboardInterrupt:
        m.shutdown()

Assuming the company proxy is at IP "1.2.3.4" port 3128 and requires a login USER and PASSWORD, how can I change this script to have mitproxy use that proxy instead of going to the internet directly?

Addition info: I am not using mitmdump with the script-parameter to run this script. The goal is to run this from Python 3.8 with a pip-installed mitmproxy

0 votes

For testing purposes I want my code to raise a socket "connection reset by peer" error, so that I can test how I handle it, but I am not sure how to raise the error.

+1 vote

I'm trying to multiply two matrices that has different size.

import numpy as np

a = np.random.randn(4, 3)
b = np.random.randn(4, 1)

print a
print b

How should I multiply a and b so that the multipled matrix's size is 4*3?

I want to multiply matrix 'b' to each row of matrix 'a'.
So that if matrix a is
[[1, 2, 3],
[2, 3, 4]]
and b is
[[2],
[3]]
a*b is
[[2, 4, 6],
[4, 6 ,8]]

Plz help me, if possible, plz use numpy

+1 vote

A list can contain different types of elements but I am not able to understand how max () method work with different types of data elements.

0 votes

I can create a list that has repeated elements of another list as follows:

xx = ["a","b"]
nrep = 3
print xx
yy = []
for aa in xx:
 for i in range(nrep):
 yy.append(aa)
print yy

output:

['a', 'b']
['a', 'a', 'a', 'b', 'b', 'b']

Is there a one-liner to create a list with repeated elements?

...