當前位置:首頁 » 編程語言 » fieldstoragepython

fieldstoragepython

發布時間: 2023-01-14 05:24:23

㈠ 如何用python實現去掉文本中的行序號(行號)

LINE_PATTERN =r'\s*\d+\s?(.*)'能給我詳細講講這個正則表達式嗎? \s 是匹配任何空白字元,*匹配前面的正則出現0次或多次,\d匹配數字,+表示數字出現一次或多次。 我不明白的是,r = c.findall('2 v=[] \n') 後為什麼r=['v=[] '],它是如何實現將數字去掉的?

㈡ python怎麼響應後端發送get,post請求的介面

測試用CGI,名字為test.py,放在apache的cgi-bin目錄下:
#!/usr/bin/Python
import cgi
def main():
print "Content-type: text/html "
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,31Mar201210:07:02GMT'),('connection','close'),('etag','"e8744-127-4bc871e4fdd80"'),('date','Mon,03Sep201210:01:47GMT'),('content-type','text/html')]

date=response.getheader('date');
print date

取出響應頭部的date的值。

******************************************************************************************************************************************************************************************************************************************************

所謂網頁抓取,就是把URL地址中指定的網路資源從網路流中讀取出來,保存到本地。
類似於使用程序模擬IE瀏覽器的功能,把URL作為HTTP請求的內容發送到伺服器端, 然後讀取伺服器端的響應資源。

在Python中,我們使用urllib2這個組件來抓取網頁。
urllib2是Python的一個獲取URLs(Uniform Resource Locators)的組件。

它以urlopen函數的形式提供了一個非常簡單的介面。

最簡單的urllib2的應用代碼只需要四行。

我們新建一個文件urllib2_test01.py來感受一下urllib2的作用:

import urllib2
response = urllib2.urlopen('http://www..com/')
html = response.read()
print html


按下F5可以看到運行的結果:

我們可以打開網路主頁,右擊,選擇查看源代碼(火狐OR谷歌瀏覽器均可),會發現也是完全一樣的內容。

也就是說,上面這四行代碼將我們訪問網路時瀏覽器收到的代碼們全部列印了出來。

這就是一個最簡單的urllib2的例子。

除了"http:",URL同樣可以使用"ftp:","file:"等等來替代。

HTTP是基於請求和應答機制的:

客戶端提出請求,服務端提供應答。

urllib2用一個Request對象來映射你提出的HTTP請求。

在它最簡單的使用形式中你將用你要請求的地址創建一個Request對象,

通過調用urlopen並傳入Request對象,將返回一個相關請求response對象,

這個應答對象如同一個文件對象,所以你可以在Response中調用.read()。

我們新建一個文件urllib2_test02.py來感受一下:

import urllib2
req = urllib2.Request('http://www..com')
response = urllib2.urlopen(req)
the_page = response.read()
print the_page

可以看到輸出的內容和test01是一樣的。

urllib2使用相同的介面處理所有的URL頭。例如你可以像下面那樣創建一個ftp請求。

req = urllib2.Request('ftp://example.com/')

在HTTP請求時,允許你做額外的兩件事。

1.發送data表單數據

這個內容相信做過Web端的都不會陌生,

有時候你希望發送一些數據到URL(通常URL與CGI[通用網關介面]腳本,或其他WEB應用程序掛接)。

在HTTP中,這個經常使用熟知的POST請求發送。

這個通常在你提交一個HTML表單時由你的瀏覽器來做。

並不是所有的POSTs都來源於表單,你能夠使用POST提交任意的數據到你自己的程序。

一般的HTML表單,data需要編碼成標准形式。然後做為data參數傳到Request對象。

編碼工作使用urllib的函數而非urllib2。

我們新建一個文件urllib2_test03.py來感受一下:

import urllib
import urllib2
url = 'http://www.someserver.com/register.cgi'
values = {'name' : 'WHY',
'location' : 'SDU',
'language' : 'Python' }
data = urllib.urlencode(values) # 編碼工作
req = urllib2.Request(url, data) # 發送請求同時傳data表單
response = urllib2.urlopen(req) #接受反饋的信息
the_page = response.read() #讀取反饋的內容

如果沒有傳送data參數,urllib2使用GET方式的請求。

GET和POST請求的不同之處是POST請求通常有"副作用",

它們會由於某種途徑改變系統狀態(例如提交成堆垃圾到你的門口)。

Data同樣可以通過在Get請求的URL本身上面編碼來傳送。

import urllib2
import urllib
data = {}
data['name'] = 'WHY'
data['location'] = 'SDU'
data['language'] = 'Python'
url_values = urllib.urlencode(data)
print url_values
name=Somebody+Here&language=Python&location=Northampton
url = 'http://www.example.com/example.cgi'
full_url = url + '?' + url_values
data = urllib2.open(full_url)

這樣就實現了Data數據的Get傳送。

2.設置Headers到http請求

有一些站點不喜歡被程序(非人為訪問)訪問,或者發送不同版本的內容到不同的瀏覽器。

默認的urllib2把自己作為「Python-urllib/x.y」(x和y是Python主版本和次版本號,例如Python-urllib/2.7),

這個身份可能會讓站點迷惑,或者乾脆不工作。

瀏覽器確認自己身份是通過User-Agent頭,當你創建了一個請求對象,你可以給他一個包含頭數據的字典。

下面的例子發送跟上面一樣的內容,但把自身模擬成Internet Explorer。

(多謝大家的提醒,現在這個Demo已經不可用了,不過原理還是那樣的)。

import urllib
import urllib2
url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'name' : 'WHY',
'location' : 'SDU',
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

以上就是python利用urllib2通過指定的URL抓取網頁內容的全部內容,非常簡單吧,希望對大家能有所幫助

㈢ python earth怎麼用

用法舉例:
import cgi

url = cgi.FieldStorage()
bbox = url['BBOX'].value
bbox = bbox.split(',')
west = float(bbox[0])
south = float(bbox[1])
east = float(bbox[2])
north = float(bbox[3])

center_lng = ((east - west) / 2) + west
center_lat = ((north - south) / 2) + south

kml = (
'<?xml version="1.0" encoding="UTF-8"?> '
'<kml xmlns="http://earth.google.com/kml/2.2"> '
'<Placemark> '
'<name>View-centered placemark</name> '
'<Point> '
'<coordinates>%.6f,%.6f</coordinates> '
'</Point> '
'</Placemark> '
'</kml>'
) %(center_lng, center_lat)

print 'Content-Type: application/vnd.google-earth.kml+xml '
print kml

㈣ 如何用PYTHON的CGIHTTPSERVER模塊模擬POST請求

這次又要逼真一點點,可以弄POST請求啦。
在WEB根目錄下新建cgi-bin目錄(據說是規模要求),然後運行命令:

1

python -m CGIHTTPServer

CGI-BIN目錄下,form.py處理POST請求的內容(簡化到不行):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# -*- coding: utf-8 -*-

import cgi

header = 'Content-Type: text/html\n\n'

html = '<h3>接受處理表單數據\n</h3>'
#列印返回的內容
#print header
#print html
# 接受表達提交的數據
form = cgi.FieldStorage()

#print '接收表達get的數據 :',form

print '<p />'

# 解析處理提交的數據
content = form['userName'].value

print content, '$$$$$$$$$$$$$'
formhtml = '''
%s
'''

print formhtml % ('登陸成功')

然後,就可以測試EXTJS中的提交表單更新HTML元素啦。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

<!DOCTYPE html>
<html>
<head>
<title>ExtJs</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="ExtJs/packages/ext-theme-crisp/build/resources/ext-theme-crisp-all.css">
<script type="text/javascript" src="ExtJs/ext-all.js"></script>
<script type="text/javascript" src="ExtJs/bootstrap.js"></script>
<script type="text/javascript" src="ExtJs/packages/ext-theme-crisp/build/ext-theme-crisp.js"></script>

<script type="text/javascript">
Ext.onReady(function(){
var loader = Ext.get("loginMsg").getLoader();
Ext.get('loginBtn').on('click', login);
function login(){
loader.load({
form: "loginForm",
url: '/cgi-bin/form.py'
});
}
});
</script>
</head>
<body style="margin: 20px">
<form id="loginForm">
用戶名:<input name="userName" type="text">
密碼:<input name="password" type="password">
<input type="button" value="登陸" id="loginBtn">
</form>
狀態:<span id="loginMsg"></span>
</body>
</html>

㈤ 如何用python寫一段代碼

簡單的,可以使用python 的CGI模塊,需要你的伺服器開啟CGI支持。

網頁內容如下:

<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
<title>pythoncgi</title>
</head><body>
<pstyle="font-size:24pt;color:red">輸入登錄密碼:</p>
<formname="zhaji"action="login.py"method="post">
<p>密碼:<inputtype="password"name="ma"size="9"></p>
<inputtype="button"name="shuru"value="登錄"onclick="zhaji.submit()">
</form>
</body></html>



login.py文件內容如下:

#!/usr/bin/python
#coding:utf-8
importcgi
form=cgi.FieldStorage()
ma=""
if'ma'inform:
ma=form["ma"].value
print'''Content-Type:text/html '''
print'''<html><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/><title>pythoncgi</title></head><body><p>你輸入的密碼是:%s</p></body></html>'''%ma

㈥ 怎麼知道python發送了什麼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") 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 = "test.py?ServiceCode=aaaa"
req = urllib2.Request(url)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res

方法二、
import httplib
url = "hest/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 = "/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 = "python_test/test.py"
headerdata = {"Host":"116"}
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("1.16",80) 與伺服器建立鏈接。
2、HTTPConnection.request(method,url[,body[,header]])函數
這個是向伺服器發送請求
method 請求的方式,一般是post或者get,
例如:
method="POST"或method="Get"
url 請求的資源,請求的資源(頁面或者CGI,我們這里是CGI)
例如:
url="htti-bin/python_test/test.py" 請求CGI
或者
url="ht_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 = "hgi-bin/python_test/test.py"
headerdata = {"Host":"192.116"}
conn = httplib.HTTPConnection("196",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的值。

㈦ 前端js 後端python 如何用ajax下載文件

前端js改成這樣試試:
var form = $("<form></form>").attr("action", "/cgi-bin/rpt_data_toExcel.py").attr("method", "post");
form.append($("<input></input>").attr("type", "hidden").attr("name", "fileName").attr("value", "results.xls"));
form.appendTo('body').submit().remove();

熱點內容
商湯科技存儲負責人 發布:2025-07-15 01:24:21 瀏覽:251
文件夾如何批量替換文件名 發布:2025-07-15 01:19:15 瀏覽:67
ftp上傳網頁 發布:2025-07-15 01:13:09 瀏覽:181
音樂文件夾圖標 發布:2025-07-15 01:03:41 瀏覽:494
安卓機怎麼反向充電 發布:2025-07-15 01:03:40 瀏覽:500
電腦使用華為雲伺服器 發布:2025-07-15 00:48:10 瀏覽:533
中考應該如何排解壓力 發布:2025-07-15 00:17:54 瀏覽:362
安卓第三方應用軟體是什麼 發布:2025-07-15 00:12:06 瀏覽:149
程序業務配置存儲 發布:2025-07-14 23:52:16 瀏覽:685
csdn編程挑戰 發布:2025-07-14 23:52:08 瀏覽:791