-1

I am trying to parse 127.0.0.1:1234 with urlparse

urlparse('127.0.0.1:1234')

and it is giving me this result

ParseResult(scheme='127.0.0.1', netloc='', path='1234', params='', query='', fragment='')

I want 127.0.0.1 in netloc field. How to do that?

Khan Asfi Reza
  • 566
  • 5
  • 28
  • 1
    It's not a valid URL so you basically get garbage in, garbage out. Did you mean `http://127.0.0.1:1234`? – tripleee Jan 14 '22 at 09:52
  • def register_node(): values = request.form # 127.0.0.1:5002,127.0.0.1:5003,127.0.0.1:5004 nodes = values.get('nodes').replace(' ', '').split(',') if nodes is None: return 'Error: Please supply a valid list of nodes', 400 for node in nodes: blockchain.register_node(node) response = { 'message': 'Nodes have been added', 'total_nodes': [node for node in blockchain.nodes] } return jsonify(response), 200 – DothraxRanger Jan 14 '22 at 10:53
  • def register_node(self, node_url): parsed_url = urlparse(node_url) if parsed_url.netloc: self.nodes.add(parsed_url.netloc) elif parsed_url.path: self.nodes.add(parsed_url.path) else: raise ValueError('Invalid URL') – DothraxRanger Jan 14 '22 at 10:53
  • Whatever you are hoping to accomplish, posting code in comments is not the way to get there. If you are trying to clarify your question, [edit] it (but I doubt adding this code will make it any clearer). – tripleee Jan 14 '22 at 10:54

1 Answers1

1

You need to add a scheme before the URL, http or https

URL = 'http://127.0.0.1:1234'
# Here http is the scheme and 127.0.0.1 is the netloc
parsed_obj = urlparse('http://127.0.0.1:1234')
print(parsed_obj)
# ParseResult(scheme='http', netloc='127.0.0.1:1234', path='', params='', query='', fragment='')
Khan Asfi Reza
  • 566
  • 5
  • 28
  • def register_node(): values = request.form # 127.0.0.1:5002,127.0.0.1:5003,127.0.0.1:5004 nodes = values.get('nodes').replace(' ', '').split(',') if nodes is None: return 'Error: Please supply a valid list of nodes', 400 for node in nodes: blockchain.register_node(node) response = { 'message': 'Nodes have been added', 'total_nodes': [node for node in blockchain.nodes] } return jsonify(response), 200 – DothraxRanger Jan 14 '22 at 10:42
  • def register_node(self, node_url): parsed_url = urlparse(node_url) if parsed_url.netloc: self.nodes.add(parsed_url.netloc) elif parsed_url.path: self.nodes.add(parsed_url.path) else: raise ValueError('Invalid URL') – DothraxRanger Jan 14 '22 at 10:43
  • Adding your code, in the comment section doesn't help. You have to specify the problem you are facing by explaining the code you have written, what type of problem you are facing, and what results you would like to get from your code. Then others can help you solve your problem, also use StackOverflow documentation to write and format code, it will be helpful for others to read and solve your problem. Thank you – Khan Asfi Reza Jan 14 '22 at 18:27