title: urllib2.URLError: <urlopen error [Errno 111] Connection refused>
body: <p>I am trying to connect to <a href="http://localhost:1438/~cs1xx/1x/007.php" rel="nofollow">http://localhost:1438/~cs1xx/1x/007.php</a> using <code>urllib2.urlopen</code>. But I get connection refused error. Can anyone help me out?</p>
  
  <p>Below are the details and what I have tried so far:</p>
  
  <pre><code>Traceback (most recent call last):
    File "main.py", line 472, in &lt;module&gt;
      php_data = urllib2.urlopen(php_test)
    File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
      return _opener.open(url, data, timeout)
    File "/usr/lib/python2.7/urllib2.py", line 404, in open
      response = self._open(req, data)
    File "/usr/lib/python2.7/urllib2.py", line 422, in _open
      '_open', req)
    File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
      result = func(*args)
    File "/usr/lib/python2.7/urllib2.py", line 1214, in http_open
      return self.do_open(httplib.HTTPConnection, req)
    File "/usr/lib/python2.7/urllib2.py", line 1184, in do_open
      raise URLError(err)
  urllib2.URLError: &lt;urlopen error [Errno 111] Connection refused&gt;
  </code></pre>
  
  <p>Here php_test is <a href="http://localhost:1438/~cs1xx/1x/007.php" rel="nofollow">http://localhost:1438/~cs1xx/1x/007.php</a>. I can access this site from my browser. I am also able to ping this using:</p>
  
  <pre><code>c$ nmap -p 1438 localhost
  
   Starting Nmap 6.40 ( http://nmap.org ) at 2016-10-30 12:52 PDT
   Nmap scan report for localhost (127.0.0.1)
   Host is up (0.00016s latency).
   Other addresses for localhost (not scanned): 127.0.0.1
   PORT     STATE  SERVICE
   1438/tcp closed eicon-server
  
   Nmap done: 1 IP address (1 host up) scanned in 0.09 seconds
  </code></pre>
  
  <p>Edit:</p>
  
  <p>Python code (only the relevant portion)</p>
  
  <pre><code>import os,sys
  import urllib,urllib2;
  php_test = submission_path + php_test_folder + test_file  # these are set somewhere
  os.system("echo " + php_test)  # prints http://localhost:1438/~cs1xx/1x/007.php
  php_data = urllib2.urlopen(php_test)
  </code></pre>