-1

I have a daemon which uploads json to http://localhost:9000, I can read json easily using hapi with this code:

'use strict';

const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection({
    host: 'localhost',
    port: 9000
});

server.route({
    method: 'POST',
    path:'/push/',
    handler: function (request, reply) {
        console.log('Server running at:', request.payload);
        return reply('hello world');
    }
});

server.start((err) => {

    if (err) {
        throw err;
    }
    console.log('Server running at:', server.info.uri);
});

but I'm trying to handle this in python, I found a answer here, so here is my code:

import urllib, json
url = 'http://localhost:9000/push/'
response = urllib.urlopen(url)
data = json.load(response.read())
print data

but when I run it I get Exception:

Traceback (most recent call last):
  File "pythonServer.py", line 3, in <module>
    response = urllib.urlopen(url)
  File "/usr/lib/python2.7/urllib.py", line 87, in urlopen
    return opener.open(url)
  File "/usr/lib/python2.7/urllib.py", line 213, in open
    return getattr(self, name)(url)
  File "/usr/lib/python2.7/urllib.py", line 350, in open_http
    h.endheaders(data)
  File "/usr/lib/python2.7/httplib.py", line 997, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 850, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 812, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 793, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
IOError: [Errno socket error] [Errno 111] Connection refused

How can I solve this?

Community
  • 1
  • 1
Yashar
  • 2,455
  • 3
  • 25
  • 31
  • can you access it from your browser, for instance? you can also make your life a lot easier by using the requests library for python. http://docs.python-requests.org/en/latest/ – pvg Dec 13 '15 at 21:35
  • you can use `netstat -aon` to check if a port is listening – PanGalactic Dec 13 '15 at 21:36
  • @PanGalactic `tcp 0 0 127.0.0.1:9000 127.0.0.1:52972 TIME_WAIT timewait (47.36/0/0)` – Yashar Dec 15 '15 at 14:13

3 Answers3

0

Notice that your JavaScript code is using the following:

method: 'POST'

Your Python code by contrast will be default to a GET method, since you didn't explicitly specify it. You can look here for how to make a POST request using urllib, but I would recommend you look into using the requests library instead.

It's a simple as

import requests
r = requests.post("http://localhost:9000/push/", data = {"key":"value"})

Alternatively, just change your server (JavaScript/Node.js code) to use method: 'GET'.

Community
  • 1
  • 1
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
0

Your server route is expecting a POST request.

Your Python code is sending a GET request.

K Cartlidge
  • 171
  • 1
  • 6
0

Your server is sending down POST request and you are fetching a GET call.

response = urllib.urlopen(url)

urllib.urlopen opens the network URL for reading.

Instead of sending a GET request you can send POST request to server. Here is the complete code:

import urllib, json, urllib2
url = 'http://localhost:9000/push/'

# This packages the request (it doesn't make it)
request = urllib2.Request(url)

# Sends the request and catches the response
response = urllib2.urlopen(request)

data = json.load(response.read())
print data
perfectus
  • 466
  • 4
  • 14