如何实现我的PHP curl请求到Python

编程入门 行业动态 更新时间:2024-10-26 22:18:09
本文介绍了如何实现我的PHP curl请求到Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的PHP代码从服务器A抓取html到服务器B.我这样做是为了规避浏览器的同一域策略。 (jQuery的JSONP也可以用来实现这个,但我更喜欢这个方法)

This PHP code below fetches html from server A to server B. I did this to circumvent the same-domain policy of browsers. (jQuery's JSONP can also be used to achieve this but I prefer this method)

<?php /* This code goes inside the body tag of server-B. Server-A then returns a set of form tags to be echoed in the body tag of Server-B */ $ch = curl_init(); $url = "server-A/form.php"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER,FALSE); curl_exec($ch); // grab URL and pass it to the browser curl_close($ch); // close cURL resource, and free up system resources ?>

如何在Python中实现?我确信在Python中还有Curl实现,但我不知道该怎么做。

How can I achieve this in Python? Im sure there is Curl implementation in Python too but I dont quite know how to do it yet.

推荐答案

有Python的cURL包装器,但首选方法是使用 urllib2

There are cURL wrappers for Python, but the preferred way of doing this is using urllib2

请注意,您的PHP代码会检索整个页面并打印。等效的Python代码是:

Note that your code in PHP retrieves the whole page and prints it. The equivalent Python code is:

import urllib2 url = 'server-A/form.php' res = urllib2.urlopen(url) print res.read()

更多推荐

如何实现我的PHP curl请求到Python

本文发布于:2023-10-26 01:12:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何实现   PHP   Python   curl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!