如题所述
æ¬æå®ä¾è®²è¿°äº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") and form["ServiceCode"].value != "":
print "<h1> Hello",form["ServiceCode"].value,"</h1>"
else:
print "<h1> Error! Please enter first name.</h1>"
main()
pythonåépoståget请æ±
get请æ±ï¼
使ç¨getæ¹å¼æ¶ï¼è¯·æ±æ°æ®ç´æ¥æ¾å¨urlä¸ã
æ¹æ³ä¸ã
import urllib
import urllib2
url = "http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa"
req = urllib2.Request(url)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
æ¹æ³äºã
import httplib
url = "http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa"
conn = httplib.HTTPConnection("192.168.81.16")
conn.request(method="GET",url=url)
response = conn.getresponse()
res= response.read()
print res
post请æ±ï¼
使ç¨postæ¹å¼æ¶ï¼æ°æ®æ¾å¨dataæè bodyä¸ï¼ä¸è½æ¾å¨urlä¸ï¼æ¾å¨urlä¸å°è¢«å¿½ç¥ã
æ¹æ³ä¸ã
import urllib
import urllib2
test_data = {'ServiceCode':'aaaa','b':'bbbbb'}
test_data_urlencode = urllib.urlencode(test_data)
requrl = "http://192.168.81.16/cgi-bin/python_test/test.py"
req = urllib2.Request(url = requrl,data =test_data_urlencode)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
æ¹æ³äºã
import urllib
import httplib
test_data = {'ServiceCode':'aaaa','b':'bbbbb'}
test_data_urlencode = urllib.urlencode(test_data)
requrl = "http://192.168.81.16/cgi-bin/python_test/test.py"
headerdata = {"Host":"192.168.81.16"}
conn = httplib.HTTPConnection("192.168.81.16")
conn.request(method="POST",url=requrl,body=test_data_urlencode,headers = headerdata)
response = conn.getresponse()
res= response.read()
print res
对pythonä¸jsonç使ç¨ä¸æ¸ æ¥ï¼æ以临æ¶ä½¿ç¨äºurllib.urlencode(test_data)æ¹æ³;
模åurllib,urllib2,httplibçåºå«
httplibå®ç°äºhttpåhttpsç客æ·ç«¯åè®®ï¼ä½æ¯å¨pythonä¸ï¼æ¨¡åurllibåurllib2对httplibè¿è¡äºæ´ä¸å±çå°è£ ã
ä»ç»ä¸ä¾åä¸ç¨å°çå½æ°ï¼
1ãHTTPConnectionå½æ°
httplib.HTTPConnection(host[,port[,stict[,timeout]]])
è¿ä¸ªæ¯æé å½æ°ï¼è¡¨ç¤ºä¸æ¬¡ä¸æå¡å¨ä¹é´ç交äºï¼å³è¯·æ±/ååº
host æ è¯æå¡å¨ä¸»æº(æå¡å¨IPæåå)
port é»è®¤å¼æ¯80
strict 模å¼æ¯Falseï¼è¡¨ç¤ºæ æ³è§£ææå¡å¨è¿åçç¶æè¡æ¶ï¼æ¯å¦æåºBadStatusLineå¼å¸¸
ä¾å¦:
conn = httplib.HTTPConnection("192.168.81.16"ï¼80) ä¸æå¡å¨å»ºç«é¾æ¥ã
2ãHTTPConnection.request(method,url[,body[,header]])å½æ°
è¿ä¸ªæ¯åæå¡å¨åé请æ±
method 请æ±çæ¹å¼ï¼ä¸è¬æ¯postæè getï¼
ä¾å¦ï¼
method="POST"æmethod="Get"
url 请æ±çèµæºï¼è¯·æ±çèµæº(页é¢æè CGI,æ们è¿éæ¯CGI)
ä¾å¦ï¼
url="http://192.168.81.16/cgi-bin/python_test/test.py" 请æ±CGI
æè
url="http://192.168.81.16/python_test/test.html" 请æ±é¡µé¢
body éè¦æ交å°æå¡å¨çæ°æ®ï¼å¯ä»¥ç¨jsonï¼ä¹å¯ä»¥ç¨ä¸é¢çæ ¼å¼ï¼jsonéè¦è°ç¨json模å
headers 请æ±çhttp头headerdata = {"Host":"192.168.81.16"}
ä¾å¦:
test_data = {'ServiceCode':'aaaa','b':'bbbbb'}
test_data_urlencode = urllib.urlencode(test_data)
requrl = "http://192.168.81.16/cgi-bin/python_test/test.py"
headerdata = {"Host":"192.168.81.16"}
conn = httplib.HTTPConnection("192.168.81.16"ï¼80)
conn.request(method="POST",url=requrl,body=test_data_urlencode,headers = headerdata)
connå¨ä½¿ç¨å®æ¯åï¼åºè¯¥å ³éï¼conn.close()
3ãHTTPConnection.getresponse()å½æ°
è¿ä¸ªæ¯è·åhttpååºï¼è¿åç对象æ¯HTTPResponseçå®ä¾ã
4ãHTTPResponseä»ç»ï¼
HTTPResponseçå±æ§å¦ä¸ï¼
read([amt]) è·åååºæ¶æ¯ä½ï¼amt表示ä»ååºæµä¸è¯»åæå®åèçæ°æ®ï¼æ²¡ææå®æ¶ï¼å°å ¨é¨æ°æ®è¯»åºï¼
getheader(name[,default]) è·å¾ååºçheaderï¼nameæ¯è¡¨ç¤ºå¤´ååï¼å¨æ²¡æ头ååçæ¶åï¼defaultç¨æ¥æå®è¿åå¼
getheaders() 以å表çå½¢å¼è·å¾header
ä¾å¦ï¼
date=response.getheader('date');
print date
resheader=''
resheader=response.getheaders();
print resheader
åå½¢å¼çååºå¤´é¨ä¿¡æ¯:
[('content-length', '295'), ('accept-ranges', 'bytes'), ('server', 'Apache'), ('last-modified', 'Sat, 31 Mar 2012 10:07:02 GMT'), ('connection', 'close'), ('etag', '"e8744-127-4bc871e4fdd80"'), ('date', 'Mon, 03 Sep 2012 10:01:47 GMT'), ('content-type', 'text/html')]
date=response.getheader('date');
print date
ååºååºå¤´é¨çdateçå¼ã
æµè¯ç¨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") and form["ServiceCode"].value != "":
print "<h1> Hello",form["ServiceCode"].value,"</h1>"
else:
print "<h1> Error! Please enter first name.</h1>"
main()
pythonåépoståget请æ±
get请æ±ï¼
使ç¨getæ¹å¼æ¶ï¼è¯·æ±æ°æ®ç´æ¥æ¾å¨urlä¸ã
æ¹æ³ä¸ã
import urllib
import urllib2
url = "http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa"
req = urllib2.Request(url)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
æ¹æ³äºã
import httplib
url = "http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa"
conn = httplib.HTTPConnection("192.168.81.16")
conn.request(method="GET",url=url)
response = conn.getresponse()
res= response.read()
print res
post请æ±ï¼
使ç¨postæ¹å¼æ¶ï¼æ°æ®æ¾å¨dataæè bodyä¸ï¼ä¸è½æ¾å¨urlä¸ï¼æ¾å¨urlä¸å°è¢«å¿½ç¥ã
æ¹æ³ä¸ã
import urllib
import urllib2
test_data = {'ServiceCode':'aaaa','b':'bbbbb'}
test_data_urlencode = urllib.urlencode(test_data)
requrl = "http://192.168.81.16/cgi-bin/python_test/test.py"
req = urllib2.Request(url = requrl,data =test_data_urlencode)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
æ¹æ³äºã
import urllib
import httplib
test_data = {'ServiceCode':'aaaa','b':'bbbbb'}
test_data_urlencode = urllib.urlencode(test_data)
requrl = "http://192.168.81.16/cgi-bin/python_test/test.py"
headerdata = {"Host":"192.168.81.16"}
conn = httplib.HTTPConnection("192.168.81.16")
conn.request(method="POST",url=requrl,body=test_data_urlencode,headers = headerdata)
response = conn.getresponse()
res= response.read()
print res
对pythonä¸jsonç使ç¨ä¸æ¸ æ¥ï¼æ以临æ¶ä½¿ç¨äºurllib.urlencode(test_data)æ¹æ³;
模åurllib,urllib2,httplibçåºå«
httplibå®ç°äºhttpåhttpsç客æ·ç«¯åè®®ï¼ä½æ¯å¨pythonä¸ï¼æ¨¡åurllibåurllib2对httplibè¿è¡äºæ´ä¸å±çå°è£ ã
ä»ç»ä¸ä¾åä¸ç¨å°çå½æ°ï¼
1ãHTTPConnectionå½æ°
httplib.HTTPConnection(host[,port[,stict[,timeout]]])
è¿ä¸ªæ¯æé å½æ°ï¼è¡¨ç¤ºä¸æ¬¡ä¸æå¡å¨ä¹é´ç交äºï¼å³è¯·æ±/ååº
host æ è¯æå¡å¨ä¸»æº(æå¡å¨IPæåå)
port é»è®¤å¼æ¯80
strict 模å¼æ¯Falseï¼è¡¨ç¤ºæ æ³è§£ææå¡å¨è¿åçç¶æè¡æ¶ï¼æ¯å¦æåºBadStatusLineå¼å¸¸
ä¾å¦:
conn = httplib.HTTPConnection("192.168.81.16"ï¼80) ä¸æå¡å¨å»ºç«é¾æ¥ã
2ãHTTPConnection.request(method,url[,body[,header]])å½æ°
è¿ä¸ªæ¯åæå¡å¨åé请æ±
method 请æ±çæ¹å¼ï¼ä¸è¬æ¯postæè getï¼
ä¾å¦ï¼
method="POST"æmethod="Get"
url 请æ±çèµæºï¼è¯·æ±çèµæº(页é¢æè CGI,æ们è¿éæ¯CGI)
ä¾å¦ï¼
url="http://192.168.81.16/cgi-bin/python_test/test.py" 请æ±CGI
æè
url="http://192.168.81.16/python_test/test.html" 请æ±é¡µé¢
body éè¦æ交å°æå¡å¨çæ°æ®ï¼å¯ä»¥ç¨jsonï¼ä¹å¯ä»¥ç¨ä¸é¢çæ ¼å¼ï¼jsonéè¦è°ç¨json模å
headers 请æ±çhttp头headerdata = {"Host":"192.168.81.16"}
ä¾å¦:
test_data = {'ServiceCode':'aaaa','b':'bbbbb'}
test_data_urlencode = urllib.urlencode(test_data)
requrl = "http://192.168.81.16/cgi-bin/python_test/test.py"
headerdata = {"Host":"192.168.81.16"}
conn = httplib.HTTPConnection("192.168.81.16"ï¼80)
conn.request(method="POST",url=requrl,body=test_data_urlencode,headers = headerdata)
connå¨ä½¿ç¨å®æ¯åï¼åºè¯¥å ³éï¼conn.close()
3ãHTTPConnection.getresponse()å½æ°
è¿ä¸ªæ¯è·åhttpååºï¼è¿åç对象æ¯HTTPResponseçå®ä¾ã
4ãHTTPResponseä»ç»ï¼
HTTPResponseçå±æ§å¦ä¸ï¼
read([amt]) è·åååºæ¶æ¯ä½ï¼amt表示ä»ååºæµä¸è¯»åæå®åèçæ°æ®ï¼æ²¡ææå®æ¶ï¼å°å ¨é¨æ°æ®è¯»åºï¼
getheader(name[,default]) è·å¾ååºçheaderï¼nameæ¯è¡¨ç¤ºå¤´ååï¼å¨æ²¡æ头ååçæ¶åï¼defaultç¨æ¥æå®è¿åå¼
getheaders() 以å表çå½¢å¼è·å¾header
ä¾å¦ï¼
date=response.getheader('date');
print date
resheader=''
resheader=response.getheaders();
print resheader
åå½¢å¼çååºå¤´é¨ä¿¡æ¯:
[('content-length', '295'), ('accept-ranges', 'bytes'), ('server', 'Apache'), ('last-modified', 'Sat, 31 Mar 2012 10:07:02 GMT'), ('connection', 'close'), ('etag', '"e8744-127-4bc871e4fdd80"'), ('date', 'Mon, 03 Sep 2012 10:01:47 GMT'), ('content-type', 'text/html')]
date=response.getheader('date');
print date
ååºååºå¤´é¨çdateçå¼ã
温馨提示:答案为网友推荐,仅供参考