I'm trying to get data from php script using a my python script:
#!/usr/bin/python
import urllib
import urllib2
url = 'https://example.com/example.php'
data = urllib.urlencode({'login' : 'mylogin', 'pwd' : 'mypass', 'data' : 'mydata'})
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
d = response.read()
print d
doesn't run with error:
ERROR=1704
php script accepts:
url: https://example.com/example.php?login=xxxxxxx&pwd=xxxxxxx&t=3
Isn't it because it is https, as described in the most voted response here: python ignore certicate validation urllib2
Related
I tried to extract a search result from this page: "http://std.stheadline.com/daily/formerly.php".
While selecting on webpage 20-Nov to 22-Nov and checking the "財經" news category check box, gives 47 results.
However, my python php codes with parameters obtained from Chrome Inspect, yield 162 results. It seems the sever did not recognize my code parameters and given me the results of ALL news categories of the latest date.
I used this codes:
import pandas as pd
url= "http://std.stheadline.com/daily/ajax/ajaxFormerly.php?startDate=2019-11-20&endDate=2019-11-22&type%5B%5D=15&keyword="
df = pd.read_json(url)
print(df.info(verbose=True))
print(df)
also tried:
url= "http://std.stheadline.com/daily/ajax/ajaxFormerly.php?startDate=2019-11-20&endDate=2019-11-22&type=15&keyword="
It uses POST request which sends parameters in body, not in url. You can't send parameters in url. You may use module requests (or urllib) to send POST requests
import requests
url = 'http://std.stheadline.com/daily/ajax/ajaxFormerly.php'
params = {
'startDate': '2019-11-20',
'endDate': '2019-11-22',
'type[]': '15',
'keyword': '',
}
r = requests.post(url, data=params)
data = r.json()
print(data['totalCount']) # 47
To load it to DataFrame you may have to use io.StringIO to create file in memory.
import requests
import pandas as pd
import io
url = 'http://std.stheadline.com/daily/ajax/ajaxFormerly.php'
params = {
'startDate': '2019-11-20',
'endDate': '2019-11-22',
'type[]': '15',
'keyword': '',
}
r = requests.post(url, data=params)
f = io.StringIO(r.text)
df = pd.read_json(f)
print(df)
I'm running this python script wherein the output of the python script will be display in web browser so I'm using PHP for this. I'm using netmiko in python for upgrading the router. The main problem is when I run the python script in CMD it perfectly works but when I run it in Web browser using some html and php not all the script is not running. It gives me the below error:
scp.SCPException: scp: c7200-ipbasek9-mz.150-1.M10.bin: No such file or directory
This is the code in python with netmiko
from netmiko import ConnectHandler, cisco
from netmiko import FileTransfer
from netmiko import SCPConn
from datetime import datetime
import sys
ip_addr = Type[0]
device_type = Type[1]
password = Type[2]
source_file = Type[3]
print ("IP:" + ip_addr + '\n'
"Model:" + device_type +'\n'
"IOS:" + source_file + '\n')
start_time = datetime.now()
s_file = source_file
d_file = source_file
net_device = {
'device_type': 'cisco_ios',
'ip': ip_addr,
'username': 'admin',
'password': password,
'port': 22,
}
ssh_conn = ConnectHandler(**net_device)
print ("\n\n")
print ("dir")
output = ssh_conn.send_command("dir")
print (">> " + output + '\n')
scp_conn = SCPConn(ssh_conn)
print ("Copying files...\nPlease wait for a while... \n")
scp_conn.scp_transfer_file(s_file, d_file)
When I run this in web browser, it stops here in the copying files and it gives me this error:
I have Raspberry Pi RFID reader which is collecting simple ID numbers from tags nearby. Those are stored as a string variable. I want to continuously send this string value over to PHP script and display it on the web page and then store it in database. How do I pass this string value over using JSON in python?
Python Script:
import serial, os, httplib, json, urllib
#pyserial setup
tagID = tagID[:8] #true string tagID - 73203842
#JSON setup
headers = { "charset" : "utf-8", "Content-Type" : "application/json" }
conn = httplib.HTTPConnection("192.168.XX.XXX")
sample = { "ID" : tagID }
sJson = json.dumps(sample, ensure_ascii = 'False')
while True:
conn.request("POST", "/test.php", sJson, headers)
response = conn.getresponse()
print(response.read())
PHP File:
<?php
$data = json_decode($_POST['results']);
echo($data);
?>
I am new to python. I have created a gui based app to insert values into database.
I have created a Rest api to handle db operations. How can i append the api URL with json created in python.
app.py
from Tkinter import *
import tkMessageBox
import json
import requests
from urllib import urlopen
top = Tk()
L1 = Label(top, text="Title")
L1.pack( side = TOP)
E1 = Entry(top, bd =5)
E1.pack(side = TOP)
L2 = Label(top, text="Author")
L2.pack( side = TOP)
E2 = Entry(top, bd =5)
E2.pack(side = TOP)
L3 = Label(top, text="Body")
L3.pack( side = TOP)
E3 = Entry(top, bd =5)
E3.pack(side = TOP)
input = E2.get();
def callfunc():
data = {"author": E2.get(),
"body" : E3.get(),
"title" : E1.get()}
data_json = json.dumps(data)
# r = requests.get('http://localhost/spritle/api.php?action=get_uses')
#url = "http://localhost/spritle/api.php?action=insert_list&data_json="
#
url = urlopen("http://localhost/spritle/api.php?action=insert_list&data_json="%data_json).read()
tkMessageBox.showinfo("Result",data_json)
SubmitButton = Button(text="Submit", fg="White", bg="#0094FF",
font=("Grobold", 10), command = callfunc)
SubmitButton.pack()
top.mainloop()
Error:
TypeError: not all arguments converted during string formatting
i AM GETTING error while appending url with data_json ?
There is an error on string formating:
Swap this:
"http://localhost/spritle/api.php?action=insert_list&data_json="%data_json
by this:
"http://localhost/spritle/api.php?action=insert_list&data_json=" + data_json
or:
"http://localhost/spritle/api.php?action=insert_list&data_json={}".format(data_json)
The following statements are equivalents:
"Python with " + "PHP"
"Python with %s" % "PHP"
"Python with {}".format("PHP")
"Python with {lang}".format(lang="PHP")
Also, I don't think sending JSON data like this via URL is a good idea. You should encode the data at least.
You are trying to use % operator to format the string, and you need to put the %s placeholder into the string:
"http://localhost/spritle/api.php?action=insert_list&data_json=%s" % data_json
Or use other methods suggested in another answer.
Regarding the data transfer - you definitely need to use POST request and not GET.
Check this, using urllib2 and this, using requests.
I am calling a php api via curl
ncServerURL='http://myserver/acertify.php'
# binaryptr = open('sampleamex.xml','rb').read()
# print binaryptr
c = pycurl.Curl()
c.setopt(pycurl.URL, ncServerURL)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
header=["Content-type: text/xml","SOAPAction:run",'Content-Type: text/xml; charset=utf-8','Content-Length: '+str(len(xmldata))]
# print header
c.setopt(pycurl.HTTPHEADER, header)
c.setopt(pycurl.POSTFIELDS, "xml="+str(xmldata))
import StringIO
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.perform()
ncServerData = b.getvalue()
return ncServerData
and posting xml data. in acertify.php and i am not able to xml data in php files , i am working on a project , what i don't know in this , how can i get curl posted data in this file .
<?php
echo "hi";
print_r($_SESSION);
print_r($_POST);
// print_r($_FILES);
?>
If you mean getting POST data in php, then at first glance looks like you are posting a single field c.setopt(pycurl.POSTFIELDS, "xml="+str(xmldata))
so it should just be $_POST['xml']
And if you mean reading data with curl as a response, then curl should have returntransfer option on execution (i'm not familiar with python syntax)