python执行get提交的方法

本文实例讲述了python执行get提交的方法。分享给大家供大家参考。具体如下:

import sys, urllib2, urllib
def addGETdata(url, data):
  """Adds data to url. Data should be a list
  or tuple consisting of 2-item
  lists or tuples of the form: (key, value).
  Items that have no key should have key set to None.
  A given key may occur more than once.
  """
  return url + '?' + urllib.urlencode(data)
zipcode = 'S2S 7U8'
url = addGETdata('http://www.yoursiteweb.com/getForecast',
         [('query', zipcode)])
print "Using URL", url
req = urllib2.Request(url)
fd = urllib2.urlopen(req)
while 1:
  data = fd.read(1024)
  if not len(data):
    break
  sys.stdout.write(data)

希望本文所述对大家的Python程序设计有所帮助。

(0)

相关推荐

  • python使用post提交数据到远程url的方法

    本文实例讲述了python使用post提交数据到远程url的方法.分享给大家供大家参考.具体如下: import sys, urllib2, urllib zipcode = "S2S 1R8" url = 'http://www.yoursiteweb.com/getForecast' data = urllib.urlencode([('query', zipcode)]) req = urllib2.Request(url) fd = urllib2.urlopen(req, d

  • Python用GET方法上传文件

    之前在osc看到一个文章讨论Get和Post的不同, 有人说不能用Get来上传文件.这就是用Get上传文件的例子,client用来发Get请求,server用来收请求.文件内容是在http请求的body内传过去的.用了不同的语言,因为我觉得各自处理起来都要方便些.而且我觉得浏览器也是可以发出这样的请求的,之后我会尝试一下. 请求端代码 复制代码 代码如下: import requests #需要安装requests with open('test.txt', 'rb') as f:     re

  • 在Python中用get()方法获取字典键值的教程

    get()方法返回给定键的值.如果键不可用,则返回默认值None. 语法 以下是get()方法的语法: dict.get(key, default=None) 参数 key -- 这是要搜索在字典中的键. default -- 这是要返回键不存在的的情况下默认值. 返回值 该方法返回一个给定键的值.如果键不可用,则返回默认值为None. 例子 下面的例子显示了get()方法的使用. #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 27} prin

  • Python的Bottle框架中实现最基本的get和post的方法的教程

    1.GET方式: # -*- coding: utf-8 -*- #!/usr/bin/python # filename: GETPOST_test.py # codedtime: 2014-9-20 19:07:04 import bottle def check_login(username, password): if username == '123' and password == '234': return True else: return False @bottle.route

  • python字典get()方法用法分析

    本文实例讲述了python字典get()方法用法.分享给大家供大家参考.具体分析如下: 如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 这里给大家分享的就是字典的get()方法. 这里我们可以用字典做一个小游戏,假设用户在终端输入字符串:"1"或者是"2"或者是"3",返回对应的内容,如果是输入其他的,则返回"error" >>> info

  • python通过post提交数据的方法

    本文实例讲述了python通过post提交数据的方法.分享给大家供大家参考.具体实现方法如下: # -*- coding: cp936 -*- import urllib2 import urllib def postHttp(name=None,tel=None,address=None, price=None,num=None,paytype=None, posttype=None,other=None): url="http://www.xxx.com/dog.php" #定义要

  • python使用urllib2提交http post请求的方法

    本文实例讲述了python使用urllib2提交http post请求的方法.分享给大家供大家参考.具体实现方法如下: #!/usr/bin/python #coding=utf-8 import urllib import urllib2 def post(url, data): req = urllib2.Request(url) data = urllib.urlencode(data) #enable cookie opener = urllib2.build_opener(urllib

  • python通过get,post方式发送http请求和接收http响应的方法

    本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法.分享给大家供大家参考.具体如下: 测试用CGI,名字为test.py,放在apache的cgi-bin目录下: #!/usr/bin/python import cgi def main(): print "Content-type: text/html\n" form = cgi.FieldStorage() if form.has_key("ServiceCode") a

  • python执行get提交的方法

    本文实例讲述了python执行get提交的方法.分享给大家供大家参考.具体如下: import sys, urllib2, urllib def addGETdata(url, data): """Adds data to url. Data should be a list or tuple consisting of 2-item lists or tuples of the form: (key, value). Items that have no key shoul

  • python执行使用shell命令方法分享

    1. os.system(shell_command) 直接在终端输出执行结果,返回执行状态0,1 此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的退出状态,如果command有执行内容,会在标准输出显示.这实际上是使用C标准库函数system()实现的. 缺点:这个函数在执行command命令时需要重新打开一个终端,并且无法保存command命令的执行结果. os.system('cat /etc/passwdqc.conf') 2. os.popen()

  • python执行系统命令4种方法与比较

    Python中执行系统命令常见的方法有以下4种 注意:以下实例代码在Python3.5下运行通过. 一.os.system方法 os.system(cmd) 在子终端运行系统命令,可以获取命令执行后的返回信息以及执行返回的状态 >>> import os >>> os.system('date') 2018年 4月 8日 星期日 19时29分13秒 CST 0 #运行状态号,0表示正确 执行后返回两行结果,第一行是结果, 第二行是执行状态信息 二.os.popen方法

  • python执行js代码的方法

    什么是js代码混淆? 正常代码 我们现在看一段js代码,代码逻辑很简单,就是拼接时间返回. function formatDate(now) { var now = new Date(1230999938); var year=now.getFullYear(); var month=now.getMonth()+1; var date=now.getDate(); var hour=now.getHours(); var minute=now.getMinutes(); var second=

  • Python执行js字符串常见方法示例

    目录 方法 1--js2py 2--execjs 3--execjs 方法 执行大型js时有点慢 特殊编码的输入或输出参数会出现报错,解决方法: 可以把输入或输出的参数用base64编码一下.base64都是英文和数字,没有特殊字符了 1--js2py pip insatll js2py # 获取执行JS的环境 context = js2py.EvalJs() # 加载执行 context.execute('放JS字符代码') 2--execjs import execjs print(exec

  • 日常整理python执行系统命令的常见方法(全)

    具体内容如下: 1 os.system 例如 ipython中运行如下命令,返回运行状态status os.system('cat /etc/passwdqc.conf') min=disabled,24,11,8,7 max=40 passphrase=3 match=4 similar=deny random=47 enforce=everyone retry=3 Out[6]: 0 2 os.popen() popen(command [, mode='r' [, bufsize]]) -

  • python 执行shell命令并将结果保存的实例

    方法1: 将shell执行的结果保存到字符串 def run_cmd(cmd): result_str='' process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) result_f = process.stdout error_f = process.stderr errors = error_f.read() if errors: pass result_str =

  • Python实现输出程序执行进度百分比的方法

    本文实例讲述了Python实现输出程序执行进度百分比的方法.分享给大家供大家参考,具体如下: 对于一些大型的Python程序,我们需要在命令行输出其百分比,显得更加友好,以免被人误会程序陷入死循环.假死的窗体. 关键是利用到不换行的输出符\r,\r的输出,将直接覆盖掉此行的内容. 比如如下的程序,是一个i从0自加的十万的过程,即使对于现在高性能的CPU也是需要几秒的时间的,我们要输出其执行时候的百分比,可以在引入sys这个包之后,利用到sys.stdout.write输出,避免原生态的print

  • python执行子进程实现进程间通信的方法

    本文实例讲述了python执行子进程实现进程间通信的方法.分享给大家供大家参考.具体实现方法如下: a.py: import subprocess, time subproc = subprocess.Popen(['c:\python31\python.exe', 'c:/b.py'], stdin=subprocess.PIPE, shell=True) time.sleep(0.5) print('start') subproc.stdin.write('data\n') subproc.

随机推荐