How To Get Result Of A HTML Page With Python - python

edit : SORRY THIS IS COMPLETELY WRONG! thanks for answers <3
hello i want to get result of my html page
my url is here : http://Xantia.rzb.IR/PG/IP
result of this page is client ip but in source i have :
<script>function getIP(json) {document.write(json.ip);}</script>
<script src="http://api.ipify.org?format=jsonp&callback=getIP"></script>
<script>function getIP(json) {document.write(json.ip);}</script>
<script src="http://api.ipify.org?format=jsonp&callback=getIP"></script>
and when i use requests.get('http://xantia/pg/ip').text i just get above source
Is there a way to get the result of this URL?
I have a weblog and I want to make an API for getting pubic IP
if can I use PHP or Django pls help because I just have python
I'm sorry because my English is very bad

From the ipify.org own sample code on the homepage, there is a block of code showing how to get the IP value in python:
# This example requires the requests library be installed. You can learn more
# about the Requests library here: http://docs.python-requests.org/en/latest/
from requests import get
ip = get('https://api.ipify.org').text
print('My public IP address is: {}'.format(ip))

Your question is very vague.
What exactly do you want to achieve?
The request delivers exactly what you request - the source code of http://xantia/pg/ip.
JavaScript is executed on client (in browser, after the response is received) and not on server (before the response is generated).
What you want to achieve exactly? Receive the public ip address of the server or the ip address of visitor of your page?
For the visitors address you can use in your server side php:
<?php
$ip = $_SERVER['REMOTE_ADDRESS'];
echo $ip;
If you want to receive your servers public address, you can do something like in your server side php:
<?php
$response = file_get_contents('http://api.ipify.org/?format=json');
$data = json_decode($response, true);
$ip = $data['ip'];
echo $ip;
Please note that this is just a simple example (which requires that allow_url_fopen is enabled in your php.ini).
Better is to use a http client library (https://packagist.org/?query=http%20client).
You should also handle exceptions - what happens if api.ipfy.org is not reachable?
If this request is made everytime your page is requested, it will slow down the response time of your server. In this case you should cache the IP somewhere - i guess the public server address will not change very often (except you are using a dynamic dns service).

Related

How do I check public IP using Python or DDNS with Cloudflare

It's any way to check my public IP addres using Python? I have had an account on Cloudflare and VPS in a home (but dynamic IP). I need to update VPN IP'S before had an account on OVH and DDNS works after migration dose not work.
requests:
import requests
response = requests.get('http://ifconfig.me')
print(response.text)
python 2:
import urllib2
response = urllib2.urlopen('http://ifconfig.me')
print response.read()
python 3:
from urllib import request
response = request.urlopen('http://ifconfig.me')
print(response.read())
There's a bunch of websites online that offer simple APIs to check the IP address you're connecting from.
As an example:
➜ ~ curl 'https://api.ipify.org?format=json'
{"ip":"1.2.3.4"}
You can use a python library (like Requests) to call the API in python code.
# download requests with `pip install requests`
import requests
res = requests.get("https://api.ipify.org?format=json")
your_ip = res.json() # {"ip" : "1.2.3.4"}
Recently I've created a simple docker image that allows you to keep you local ip in sync with Cloudflare AKA Cloudflare Dynamic DNS (DDNS)
check it out here:
https://github.com/marcelowa/cloudflare-dynamic-dns

Airplay Remote control HTTP request

I use Airplay with a Raspberry Pi, so I installed Sharply-sync.
It work perfectly but I want to control the music of my iPhone which is emitting music with airplay.
I saw this page to do this: http://nto.github.io/AirPlay.html#audio-remotecontrol
So I have to make an HTTP request to control the music, but I don't know how to use this: GET /ctrl-int/1/pause HTTP/1.1
Host: starlight.local.
Active-Remote: 1986535575
How can I make a request ( why not in Python :) ) with that ?
My raspberry informations: IP: 192.168.X.XX
The Port: 5000
Active-Remote: 1075873687 (It's an example because it change every Time)
I know the first part of the url which I have to make the request, I think it's: http://192.168.X.XX:5000//ctrl-int/1/pause but I don't know how to put the rest...
How can I do that PLEASE ?
Thank for your answers and I'm so sorry for my bad English..
Just use requests module. Check sample code below.
import requests
headers = {'Host': 'starlight.local.' , 'Active-Remote': 1986535575}
url = "http://192.168.x.xx:5000//ctrl-int/1/pause"
requests.get(url, headers=headers)
For more details please check the documentation for the module at http://docs.python-requests.org/en/master/.

pysimplesoap web service return connection refused

I've created some web services using pysimplesoap like on this documentation:
https://code.google.com/p/pysimplesoap/wiki/SoapServer
When I tested it, I called it like this:
from SOAPpy import SOAPProxy
from SOAPpy import Types
namespace = "http://localhost:8008"
url = "http://localhost:8008"
proxy = SOAPProxy(url, namespace)
response = proxy.dummy(times=5, name="test")
print response
And it worked for all of my web services, but when I try to call it by using an library which is needed to specify the WSDL, it returns "Could not connect to host".
To solve my problem, I used the object ".wsdl()" to generate the correct WSDL and saved it into a file, the WSDL generated by default wasn't correct, was missing variable types and the correct server address...
The server name localhost is only meaningful on your computer. Once outside, other computers won't be able to see it.
1) find out your external IP, with http://www.whatismyip.com/ or another service. Note that IPs change over time.
2) plug the IP in to http://www.soapclient.com/soaptest.html
If your local service is answering IP requests as well as from localhost, you're done!

Finding network (external) IP addresses using Python

I want to know my internet provider (external) IP address (broadband or something else) with Python.
There are multiple machines are connected to that network. I tried in different way's but I got only the local and public IP my machine. How do I find my external IP address through Python?
Thanks in advance.
Use this script :
import urllib, json
data = json.loads(urllib.urlopen("http://ip.jsontest.com/").read())
print data["ip"]
Without json :
import urllib, re
data = re.search('"([0-9.]*)"', urllib.urlopen("http://ip.jsontest.com/").read()).group(1)
print data
Other way it was to parse ifconfig (= linux) or ipconfig (= windows) command but take care with translated Windows System
(ipconfig was translated).
Example of lib for linux :
ifparser.
Secure option (with https support)
from requests import get
get('https://ipapi.co/ip/').text
Complete JSON response
P.S. requests module is convenient for https support. You can try httplib though.
You'll have to use an external source you trust. Python2.x:
from urllib import urlopen
import json
url = 'http://api.hostip.info/get_json.php'
info = json.loads(urlopen(url).read())
print(info['ip'])
If you want more info, you can print more values from info.
Non-python oneliner:
wget -q -O- icanhazip.com
You can check out this answer:
https://stackoverflow.com/a/22157882/5687894
TL;DR:
import ipgetter
ip=ipgetter.myip()
In my opinion the simplest solution is
f = requests.request('GET', 'http://myip.dnsomatic.com')
ip = f.text
Thats all.
my website http;//botliam.com/1.php outputs your ip so you only need these 3 lines to get your ip.
import requests
page = requests.get('http://botliam.com/1.php')
ip = page.text
what its doing is:
opens my webpage and calls it "page"
sets "ip" to the contents of the "page"
if you want your own server to return your external ip instead of relying on my site the code for it is below:
<?php
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
echo "$ip";
?>

Http proxy works with urllib.urlopen, but not with requests.get [duplicate]

I am trying to do a simple get request through a proxy server:
import requests
test=requests.get("http://google.com", proxies={"http": "112.5.254.30:80"})
print test.text
The address of the proxy server in the code is just from some freely available proxy lists on the internet. The point is that this same proxy server works when I use it from browser, but it doesn't work from this program. And i tried many different proxy servers and none of them works through above code.
Here is what I get for this proxy server:
The requested URL could not be retrieved While trying to retrieve the URL: http:/// The following error was encountered:
Unable to determine IP address from host name for
The dnsserver returned: Invalid hostname
This means that: The cache was not able to resolve the
hostname presented in the URL. Check if the address is correct.
I know its an old question, but it should be
import requests
test=requests.get("http://google.com", proxies={"http":"http://112.5.254.30:80","https": "http://112.5.254.30:80"})
print (test.text)

Categories