GET request works in postman and nowhere else

I'm attempting to build a little script that will pull elevation data from the USGS's Elevation Point Query Service.

I've used urllib and requests and both hang. I've tried passing parameters and hardcoding and both hang. curl hangs. wget hangs. However when I use postman it magically works. But only inside postman. When I copy the python code produced by postman and run it, it hangs. At this point, I have no idea why it won't work outside postman. For reference, here's the exact code postman produces:

import requests

url = "http://nationalmap.gov/epqs.php"

querystring = {"x":"-75.981746", "y":"36.389272", "units":"meters", "output":"json"}

payload = ""

headers = { 'cache-control': "no-cache", 'Postman-Token': "eb7f7248-4462-4ac5-8720-c2d3f7d60e02" }

response = requests.request("GET", url, data=payload, headers=headers, params=querystring)

I'm pretty new to using requests and urllib, so if anyone has an idea, insight, or alternatives, I'd appreciate it.

Posted Feb 19, 2019, 1:08 AM UTC · edited

16
Laurielounge•

Works for me.

​

response.status_code = 200

response.ok = True

response.text

' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"\n "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">\n<html lang="en" dir="ltr">\n<head profile="http://www.w3.org/1999/xhtml/vocab">\n <meta charset="utf-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n <meta http-equiv="X-UA-Compatible" content="IE=Edge" />\n \n \n <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n<script type="text/x-mathjax-config">MathJax.Hub.Config({\r\n extensions: [\'tex2jax.js\'],\r\n jax: [\'input/TeX\',\'output/HTML-CSS\'],\r\n tex2jax: {\r\n inlineMath: [ [\'$math$\',\'$math$\'], [\'\\\\(\',\'\\\\)\'] ],\r\n processEscapes: true,\r\n processClass: \'tex2jax\',\r\n ignoreClass: \'html\'\r\n },\r\n showProcessingMessages: false,\r\n messageStyle: \'none\'\r\n});\r\n</script><meta name="abstract" content="As one of the cornerstones of the U.S. Geological Survey&#039;s

​

I would normally write it:

response = requests.get(url,data=payload,headers=headers,params=querystring)

1
InscrutablePhilOP•

And that's part of what's driving me nuts because I started putting it together last night and I swear it worked then. In fact I distinctly remember seeing each request roll across the screen and thinking, "This is taking a while. This might be a good time to figure out how to thread."

1
Laurielounge•

Took about five seconds or so to do the get. Didn't think to check the size of the text. Nothing wrong with the code.

Sure you're using python3? I ran this in python3.6.

Might try a

#!/usr/bin/env python3.6

... at the top of your code.

1
InscrutablePhilOP•

Python 3.7.2

I tried it on another computer that only has python 3 installed. Same thing.

This has to be on my end. I'll try it on someone else's wifi tomorrow.

1
Laurielounge•

Roger that. Good luck

1
DeadlyViper•

When I copy the python code produced by postman and run it, it hangs

How long did you wait for it to complete? perhaps it will timeout if you wait more and you'll get a nice error message.

Besides that the only idea i have is some sort of firewall you have , either on your computer or on the way from your computer to the internet that maybe inspects user-agents, and the application making the request (in case its a local firewall).

1