python2 httplib 笔记

python2  httplib 笔记

#coding=utf-8
‘‘‘
Created on 2014年9月25日

@author: cocoajin
‘‘‘

import httplib,urllib

base=‘httpbin.org‘ #不需要添加 "http://"
con=httplib.HTTPConnection(base)
ip = ‘/ip‘
con.request(‘GET‘,ip)
re=con.getresponse()
print re.getheaders()
print re.read()
con.close()

#GET
con=httplib.HTTPConnection(base)
parm={‘name‘:‘nick‘,‘age‘:18}
gets=‘/get‘
con.request(‘GET‘, gets+‘?‘+urllib.urlencode(parm))
re=con.getresponse()
print re.getheaders()
print re.read()
con.close()

#POST
con=httplib.HTTPConnection(base)
parm={‘name‘:‘nick‘,‘age‘:18}
posts=‘/post‘
#headers = {"Content-type":"application/json","Accept":"text/plain"} json
headers = {"Content-type":"application/json","Accept":"text/plain"} #form
con.request(‘POST‘, posts,urllib.urlencode(parm),headers)
re=con.getresponse()
print re.getheaders()
print re.read()
con.close()

#文件上传使用 "multipart/form-data"
时间: 2024-10-08 01:56:50

python2 httplib 笔记的相关文章

回味Python2.7——笔记4

一.Python 标准库概览 1.操作系统接口 os 模块提供了很多与操作系统交互的函数: >>> import os >>> os.getcwd() # Return the current working directory 'C:\\Python27' >>> os.chdir('/server/accesslogs') # Change current working directory >>> os.system('mkdi

Effective Python2 读书笔记1

Item 2: Follow the PEP 8 Style Guide Naming Naming functions, variables, attributes lowercase_underscore protected instance attributes _leading_underscore private instance attributes __double_leading_underscore classes, exceptions CapitalizedWord mod

回味Python2.7——笔记3

一.错误和异常 1.异常处理 >>> while True: ... try: ... x = int(raw_input("Please enter a number: ")) ... break ... except ValueError: ... print "Oops! That was no valid number. Try again..." ... try 语句按如下方式工作: 首先,执行 try 子句 (在 try 和 excep

Effective Python2 读书笔记2

Item 14: Prefer Exceptions to Returning None Functions that returns None to indicate special meaning are error prone because None and other values (e.g., zero, the empty string) all evaluate to False in conditional expressions. Raise exceptions to in

回味Python2.7——笔记2

一.模块 模块是包括 Python 定义和声明的文件.文件名就是模块名加上 .py 后缀.模块的模块名(做为一个字符串)可以由全局变量 __name__ 得到. 1. 模块可以导入其他的模块. 一个(好的)习惯是将所有的 import 语句放在模块的开始(或者是脚本),这并非强制. 被导入的模块名会放入当前模块的全局符号表中. from fibo import * :这样可以导入所有除了以下划线( _ )开头的命名. 需要注意的是在实践中往往不鼓励从一个模块或包中使用 * 导入所有,因为这样会让

python2 urllib 笔记

import urllib base='http://httpbin.org/' ip=base+'ip' r=urllib.urlopen(ip) print r.geturl() print r.read() #get get=base+"get" parms=urllib.urlencode({"name":"tom","age":18}) r=urllib.urlopen("%s?%s"%(get,

python内建模块发起HTTP(S)请求

一.Python2 httplib 简介:httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. httplib实现http请求 import httplib host = ‘www.baidu.com’  # 注意:不能带上协议 port = 80 # 获取HTTPConnection对象 conn = httplib.HTTPConnection(host, port) # 发起请求 co

OpenCV学习笔记(二十):Win8.1 64位+OpenCV 2.4.9+Python2.7.9配置

OpenCV提供了Python接口,主要特性包括: 提供与OpenCV 2.x中最新的C++接口极为相似的Python接口,并且包括C++中不包括的C接口 提供对OpenCV 2.x中所有主要部件的绑定:CxCORE (almost complete), CxFLANN (complete), Cv (complete), CvAux (C++ part almost complete, C part in progress), CvVidSurv (complete), HighGui (co

【学习笔记】python2和python3的input()

python2中的input()只接受变量作为传入值,非变量内容会报错. 1 >>> user=input("Enter your name:") 2 Enter your name:Kaito 3 Traceback (most recent call last): 4 File "<stdin>", line 1, in <module> 5 File "<string>", line 1