Python论坛  - 讨论区

标题:[python-chinese] 如何用python模拟一个上传文件的表单提交?

2007年06月06日 星期三 11:39

马踏飞燕 honeyday.mj在gmail.com
星期三 六月 6 11:39:37 HKT 2007

就是用python提供的urllib这类的库来模拟一个上传文件的表单的提交动作,把本地文件提交到服务器上。

应该怎么写呢?
一般的urllib.urlopen用post打开的url怎么把文件的数据流也写过去呢?
接收方是用django写的,谢谢啦。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 11:44

jessinio smith jessinio在gmail.com
星期三 六月 6 11:44:37 HKT 2007

ÄãÏë×öһϱ¾µØºÍ·þÎñÆ÷¶ËͨÐŵĶ«Î÷?
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070606/7fdbcc1a/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 12:17

马踏飞燕 honeyday.mj在gmail.com
星期三 六月 6 12:17:37 HKT 2007

在 07-6-6,jessinio smith<jessinio在gmail.com> 写道:
> 你想做一下本地和服务器端通信的东西?
> _______________________________________________

对的。
就相当于模拟一个IE的FILE类型的input框,主要是我想做一个批量上传文件的程序,直接写一个循环把大量的文件上传到服务器上。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 12:23

Zoom.Quiet zoom.quiet在gmail.com
星期三 六月 6 12:23:56 HKT 2007

On 6/6/07, 马踏飞燕 <honeyday.mj在gmail.com> wrote:
> 在 07-6-6,jessinio smith<jessinio在gmail.com> 写道:
> > 你想做一下本地和服务器端通信的东西?
> > _______________________________________________
>
> 对的。
> 就相当于模拟一个IE的FILE类型的input框,主要是我想做一个批量上传文件的程序,直接写一个循环把大量的文件上传到服务器上。
> _______________________________________________
大量文件上传的话使用 socket 稳定连接,
FTP等等协议进行吧,HTTP 的实在不靠谱,而且也不安全

> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese


-- 
'''Time is unimportant, only life important!
http://zoomquiet.org
blog在http://blog.zoomquiet.org/pyblosxom/
wiki在http://wiki.woodpecker.org.cn/moin/ZoomQuiet
scrap在http://floss.zoomquiet.org
douban在http://www.douban.com/people/zoomq/
____________________________________
Pls. use OpenOffice.org to replace M$ Office.
     http://zh.openoffice.org
Pls. use 7-zip to replace WinRAR/WinZip.
     http://7-zip.org/zh-cn/
You can get the truely Freedom 4 software.
'''

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 12:24

jessinio smith jessinio在gmail.com
星期三 六月 6 12:24:15 HKT 2007

ÎÒÒ²ÏëÖªµÀÄܲ»ÄÜ.
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070606/8aa54185/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 12:27

jessinio smith jessinio在gmail.com
星期三 六月 6 12:27:59 HKT 2007

Èç¹ûÊÇÉÙÁ¿Êý¾Ý¿âÄØ??Ôõô×ö??¸øµã´úÂë¿´Ò»ÏÂÂï~!~
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070606/f2d9ef87/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 13:52

马踏飞燕 honeyday.mj在gmail.com
星期三 六月 6 13:52:09 HKT 2007

在 07-6-6,Zoom. Quiet<zoom.quiet在gmail.com> 写道:
> On 6/6/07, 马踏飞燕 <honeyday.mj在gmail.com> wrote:
> > 在 07-6-6,jessinio smith<jessinio在gmail.com> 写道:
> > > 你想做一下本地和服务器端通信的东西?
> > > _______________________________________________
> >
> > 对的。
> > 就相当于模拟一个IE的FILE类型的input框,主要是我想做一个批量上传文件的程序,直接写一个循环把大量的文件上传到服务器上。
> > _______________________________________________
> 大量文件上传的话使用 socket 稳定连接,
> FTP等等协议进行吧,HTTP 的实在不靠谱,而且也不安全
>

道理是这样,不过现在web应用已经开发好了,就是编辑抱怨老是在网页上一个一个的选文件,太烦了,所以想做一个小工具,直接批量的把文件传到server上就好了阿,毕竟不是所有的人都喜欢用ftp的,呵呵。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 14:01

junyi sun ccnusjy在gmail.com
星期三 六月 6 14:01:23 HKT 2007

flashÓиö¿Ø¼þ¿ÉÒÔÅúÁ¿Ñ¡Öв¢ÇÒÉÏ´«
ºÜ¶àÊÓƵÍøÕ¾µÄÉÏ´«¾ÍÊÇÓÃËü×öµÄ


On 6/6/07, Âí̤·ÉÑà <honeyday.mj在gmail.com> wrote:
>
> ÔÚ 07-6-6£¬Zoom. Quiet<zoom.quiet在gmail.com> дµÀ£º
> > On 6/6/07, Âí̤·ÉÑà <honeyday.mj在gmail.com> wrote:
> > > ÔÚ 07-6-6£¬jessinio smith<jessinio在gmail.com> дµÀ£º
> > > > ÄãÏë×öһϱ¾µØºÍ·þÎñÆ÷¶ËͨÐŵĶ«Î÷?
> > > > _______________________________________________
> > >
> > > ¶ÔµÄ¡£
> > > ¾ÍÏ൱ÓÚÄ£ÄâÒ»¸öIEµÄFILEÀàÐ͵Äinput¿ò£¬Ö÷ÒªÊÇÎÒÏë×öÒ»¸öÅúÁ¿ÉÏ´«ÎļþµÄ³ÌÐò£¬Ö±½Óдһ¸öÑ­»·°Ñ´óÁ¿µÄÎļþÉÏ´«µ½·þÎñÆ÷ÉÏ¡£
> > > _______________________________________________
> > ´óÁ¿ÎļþÉÏ´«µÄ»°Ê¹Óà socket Îȶ¨Á¬½Ó£¬
> > FTPµÈµÈЭÒé½øÐаɣ¬HTTP µÄʵÔÚ²»¿¿Æ×£¬¶øÇÒÒ²²»°²È«
> >
>
>
> µÀÀíÊÇÕâÑù£¬²»¹ýÏÖÔÚwebÓ¦ÓÃÒѾ­¿ª·¢ºÃÁË£¬¾ÍÊDZ༭±§Ô¹ÀÏÊÇÔÚÍøÒ³ÉÏÒ»¸öÒ»¸öµÄÑ¡Îļþ£¬Ì«·³ÁË£¬ËùÒÔÏë×öÒ»¸öС¹¤¾ß£¬Ö±½ÓÅúÁ¿µÄ°ÑÎļþ´«µ½serverÉϾͺÃÁË°¢£¬±Ï¾¹²»ÊÇËùÓеÄÈ˶¼Ï²»¶ÓÃftpµÄ£¬ºÇºÇ¡£
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070606/a6da9d57/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 14:05

weizi nmweizi在163.com
星期三 六月 6 14:05:18 HKT 2007

这样的tuple((1,1),(2,2),(3,3))如何转换成list [[1,1],[2,2],[3,3]]

Aaa = []
A = ((1,1),(2,2),(3,3))
for aa in a:
    aaa.append(list(aa))

这样太难看了,有没有更优雅的做法。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 14:15

黄毅 yi.codeplayer在gmail.com
星期三 六月 6 14:15:31 HKT 2007

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on
win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> def to_list(t):
...   return [i if not isinstance(i, tuple) else to_list(i) for i in t]
...
>>> to_list( ((1,2), (2,3)) )
[[1, 2], [2, 3]]

On 6/6/07, weizi <nmweizi at 163.com> wrote:
>
> 这样的tuple((1,1),(2,2),(3,3))如何转换成list [[1,1],[2,2],[3,3]]
>
> Aaa = []
> A = ((1,1),(2,2),(3,3))
> for aa in a:
>    aaa.append(list(aa))
>
> 这样太难看了,有没有更优雅的做法。
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070606/a93f16d5/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 14:18

wang_zheng_yong wang_zheng_yong在163.com
星期三 六月 6 14:18:05 HKT 2007

t = ((2,3),(3,4))

print t

q = [[i,j] for i,j in t]

print q






wang_zheng_yong
2007-06-06



·¢¼þÈË£º weizi
·¢ËÍʱ¼ä£º 2007-06-06 14:05:32
ÊÕ¼þÈË£º python-chinese在lists.python.cn
³­ËÍ£º 
Ö÷Ì⣺ [python-chinese]tupleÈçºÎת»»³Élist£¿

ÕâÑùµÄtuple£¨(1,1),(2,2),(3,3))ÈçºÎת»»³Élist [[1,1],[2,2],[3,3]]

Aaa = []
A = £¨(1,1),(2,2),(3,3))
for aa in a:
    aaa.append(list(aa))

ÕâÑùÌ«ÄÑ¿´ÁË£¬ÓÐûÓиüÓÅÑŵÄ×ö·¨¡£
_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070606/467ab54d/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 14:31

huanghao.c huanghao.c在gmail.com
星期三 六月 6 14:31:26 HKT 2007

aaa = [list(tuple) for tuple in a]

weizi 写道:
> 这样的tuple((1,1),(2,2),(3,3))如何转换成list [[1,1],[2,2],[3,3]]
>
> Aaa = []
> A = ((1,1),(2,2),(3,3))
> for aa in a:
>     aaa.append(list(aa))
>
> 这样太难看了,有没有更优雅的做法。
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 16:41

weizi nmweizi在163.com
星期三 六月 6 16:41:59 HKT 2007

非常感谢。可以了。

> -----邮件原件-----
> 发件人: python-chinese-bounces在lists.python.cn
> [mailto:python-chinese-bounces在lists.python.cn] 代表 wang_zheng_yong
> 发送时间: 2007年6月6日 14:18
> 收件人: python-chinese在lists.python.cn
> 主题: Re: [python-chinese] tuple如何转换成list?
> 
> t = ((2,3),(3,4))
> 
> print t
> 
> q = [[i,j] for i,j in t]
> 
> print q
> 
> 
> 
> ________________________________
> 
> wang_zheng_yong
> 2007-06-06
> ________________________________
> 
> 发件人: weizi
> 发送时间: 2007-06-06 14:05:32
> 收件人: python-chinese在lists.python.cn
> 抄送:
> 主题: [python-chinese]tuple如何转换成list?
> 
> 这样的tuple((1,1),(2,2),(3,3))如何转换成list [[1,1],[2,2],[3,3]]
> 
> Aaa = []
> A = ((1,1),(2,2),(3,3))
> for aa in a:
>     aaa.append(list(aa))
> 
> 这样太难看了,有没有更优雅的做法。
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 16:43

Nicholas Ding nicholasdsj在gmail.com
星期三 六月 6 16:43:52 HKT 2007

Â¥Ö÷ÒªµÄÕâ¸ö¹¦ÄÜͦ³£Óõģ¬²»¾ÍÊǸöÎļþÉÏ´«ÂÓÃÏÂÃæµÄ´úÂë¾Í³É¡£

import MultipartPostHandler, urllib2, cookielib

cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
                                MultipartPostHandler.MultipartPostHandler)
params = { "username" : "bob", "password" : "riviera",
           "file" : open("filename", "rb") }
opener.open("http://wwww.bobsite.com/upload/", params)

The code is at: http://odin.himinbi.org/MultipartPostHandler.py

On 6/6/07, junyi sun <ccnusjy在gmail.com> wrote:
>
> flashÓиö¿Ø¼þ¿ÉÒÔÅúÁ¿Ñ¡Öв¢ÇÒÉÏ´«
> ºÜ¶àÊÓƵÍøÕ¾µÄÉÏ´«¾ÍÊÇÓÃËü×öµÄ
>
>
> On 6/6/07, Âí̤·ÉÑà <honeyday.mj在gmail.com> wrote:
> >
> > ÔÚ 07-6-6£¬Zoom. Quiet<zoom.quiet在gmail.com> дµÀ£º
> > > On 6/6/07, Âí̤·ÉÑà < honeyday.mj在gmail.com> wrote:
> > > > ÔÚ 07-6-6£¬jessinio smith<jessinio在gmail.com> дµÀ£º
> > > > > ÄãÏë×öһϱ¾µØºÍ·þÎñÆ÷¶ËͨÐŵĶ«Î÷?
> > > > > _______________________________________________
> > > >
> > > > ¶ÔµÄ¡£
> > > > ¾ÍÏ൱ÓÚÄ£ÄâÒ»¸öIEµÄFILEÀàÐ͵Äinput¿ò£¬Ö÷ÒªÊÇÎÒÏë×öÒ»¸öÅúÁ¿ÉÏ´«ÎļþµÄ³ÌÐò£¬Ö±½Óдһ¸öÑ­»·°Ñ´óÁ¿µÄÎļþÉÏ´«µ½·þÎñÆ÷ÉÏ¡£
> > > > _______________________________________________
> > > ´óÁ¿ÎļþÉÏ´«µÄ»°Ê¹Óà socket Îȶ¨Á¬½Ó£¬
> > > FTPµÈµÈЭÒé½øÐаɣ¬HTTP µÄʵÔÚ²»¿¿Æ×£¬¶øÇÒÒ²²»°²È«
> > >
> >
> >
> > µÀÀíÊÇÕâÑù£¬²»¹ýÏÖÔÚwebÓ¦ÓÃÒѾ­¿ª·¢ºÃÁË£¬¾ÍÊDZ༭±§Ô¹ÀÏÊÇÔÚÍøÒ³ÉÏÒ»¸öÒ»¸öµÄÑ¡Îļþ£¬Ì«·³ÁË£¬ËùÒÔÏë×öÒ»¸öС¹¤¾ß£¬Ö±½ÓÅúÁ¿µÄ°ÑÎļþ´«µ½serverÉϾͺÃÁË°¢£¬±Ï¾¹²»ÊÇËùÓеÄÈ˶¼Ï²»¶ÓÃftpµÄ£¬ºÇºÇ¡£
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
Nicholas @ Nirvana Studio
http://www.nirvanastudio.org
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070606/8be63c19/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 17:51

Ray Stinger lichray在gmail.com
星期三 六月 6 17:51:13 HKT 2007

黄毅 wrote:
> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit 
> (Intel)] on win
> 32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> def to_list(t):
> ...   return [i if not isinstance(i, tuple) else to_list(i) for i in t]
> ...
> >>> to_list( ((1,2), (2,3)) )
> [[1, 2], [2, 3]]
>
这个非常高超。
弱弱地问一句:在邮件列表里怎么回别人的帖子啊?

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 18:13

散漫酷男孩 17mxnet在gmail.com
星期三 六月 6 18:13:38 HKT 2007

给你代码参考!

import socket, string

CRLF = "\r\n"

def http_get(host, port = 80, document = "/"):

	try:
		http = miniclient(host,	port)

	except Exception, e:

		if e[0]	== 111:
			print	"Connection refused by server %s on port %d" % (host,port)

		raise

	http.writeline("GET %s HTTP/1.1" % str(document))
	http.writeline("HOST: %s" % host)
	http.writeline("User-Agent:	GameSpyHTTP/1.0")
	http.writeline("Connection: close") # do not keep-alive
	http.writeline("")
	http.shutdown() # be nice, tell the http server we're done sending the request
	
	# Determine Status
	status = string.split(http.readline())
	if status[0] != "HTTP/1.1":
		print "MiniClient: Unknown status response (%s)" % str(status[0])
	
	try:
		status = string.atoi(status[1])
	except ValueError:
		print "MiniClient: Non-numeric status code (%s)" % str(status[1])
	
	#Extract Headers
	headers = []
	while 1:
		line = http.readline()
		if not line:
			break
		headers.append(line)
	
	http.close() # all done

	#Check we got a valid HTTP response
	if status == 200:
		return http.read()
	else:
		return "E\nH\terr\nD\tHTTP Error\n$\tERR\t$"
	


def http_postSnapshot(host, port = 80, document = "/", snapshot = ""):

	try:
		http = miniclient(host,	port)

	except Exception, e:

		if e[0]	== 111:
			print	"Connection refused by server %s on port %d" % (host,port)
		
		raise

	try:
		http.writeline("POST %s	HTTP/1.1" % str(document))
		http.writeline("HOST:	%s" % str(host))
		http.writeline("User-Agent: GameSpyHTTP/1.0")
		http.writeline("Content-Type: application/x-www-form-urlencoded")
		http.writeline("Content-Length:	%s" % str(len(snapshot)))
		http.writeline("Connection: close")
		http.writeline("")
		http.writeline(str(snapshot))
		http.writeline("")
		http.shutdown() # be nice, tell the http server we're done sending the request

		# Check that SnapShot Arrives.
		# Determine Status
		status = string.split(http.readline())
		if status[0] != "HTTP/1.1":
			print "MiniClient: Unknown status response (%s)" % str(status[0])
		
		try:
			status = string.atoi(status[1])
		except ValueError:
			print "MiniClient: Non-numeric status code (%s)" % str(status[1])
		
		#Extract Headers
		headers = []
		while 1:
			line = http.readline()
			if not line:
				break
			headers.append(line)
			
		http.close() # all done
		
		if status == 200:
			print "SNAPSHOT Received: OK"
			returnCode = 1
		else:
			print "SNAPSHOT Received: ERROR"
			returnCode = 0
		
		return status

	except Exception, e:
		raise

class miniclient:
	"Client support class for simple Internet protocols."

	def __init__(self, host, port):
		"Connect to an Internet server."
		

		self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.sock.settimeout(30)

		try:
			self.sock.connect((host, port))
			self.file = self.sock.makefile("rb")

		except socket.error, e:

			#if e[0]	== 111:
			#	print	"Connection refused by server %s on port %d" % (host,port)

			raise


	def writeline(self, line):
		"Send a line to the server."
		
		try:
			self.sock.send(line + CRLF) # unbuffered write

		except socket.error, e:
			if e[0] == 32 : #broken pipe
				self.sock.close() # mutual close
				self.sock = None
			
			raise e

		except socket.timeout:
			self.sock.close() # mutual close
			self.sock = None

			raise

	def readline(self):
		"Read a line from the server.  Strip trailing CR and/or LF."

		
		s = self.file.readline()
		
		if not s:
			raise EOFError
		
		if s[-2:] == CRLF:
			s = s[:-2]
		
		elif s[-1:] in CRLF:
			s = s[:-1]
		
		return s


	def read(self, maxbytes = None):
		"Read data from server."
		
		if maxbytes is None:
			return self.file.read()
		
		else:
			return self.file.read(maxbytes)


	def shutdown(self):
		
		if self.sock:
			self.sock.shutdown(1)


	def close(self):
		
		if self.sock:
			self.sock.close()
			self.sock = None

在 07-6-6,马踏飞燕<honeyday.mj在gmail.com> 写道:
> 就是用python提供的urllib这类的库来模拟一个上传文件的表单的提交动作,把本地文件提交到服务器上。
>
> 应该怎么写呢?
> 一般的urllib.urlopen用post打开的url怎么把文件的数据流也写过去呢?
> 接收方是用django写的,谢谢啦。
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 18:18

Nicholas Ding nicholasdsj在gmail.com
星期三 六月 6 18:18:34 HKT 2007

Â¥ÉϵĴúÂëÊDz»¶ÔµÄ£¬ÎļþÉÏ´«ºÍPOST²»Ì«Ò»Ñù£¬ÄãÐèҪʹÓÃMultipart£¬²»È»ÄãÊDz»¿ÉÄÜÈÃdjangoÓÃrequest.FILESÀ´»ñÈ¡µÄ¡£

On 6/6/07, É¢Âþ¿áÄк¢ <17mxnet在gmail.com> wrote:
>
> ¸øÄã´úÂë²Î¿¼!
>
> import socket, string
>
> CRLF = "\r\n"
>
> def http_get(host, port = 80, document = "/"):
>
>         try:
>                 http = miniclient(host, port)
>
>         except Exception, e:
>
>                 if e[0] == 111:
>                         print   "Connection refused by server %s on port
> %d" % (host,port)
>
>                 raise
>
>         http.writeline("GET %s HTTP/1.1" % str(document))
>         http.writeline("HOST: %s" % host)
>         http.writeline("User-Agent:     GameSpyHTTP/1.0")
>         http.writeline("Connection: close") # do not keep-alive
>         http.writeline("")
>         http.shutdown() # be nice, tell the http server we're done sending
> the request
>
>         # Determine Status
>         status = string.split(http.readline())
>         if status[0] != "HTTP/1.1":
>                 print "MiniClient: Unknown status response (%s)" %
> str(status[0])
>
>         try:
>                 status = string.atoi(status[1])
>         except ValueError:
>                 print "MiniClient: Non-numeric status code (%s)" %
> str(status[1])
>
>         #Extract Headers
>         headers = []
>         while 1:
>                 line = http.readline()
>                 if not line:
>                         break
>                 headers.append(line)
>
>         http.close() # all done
>
>         #Check we got a valid HTTP response
>         if status == 200:
>                 return http.read()
>         else:
>                 return "E\nH\terr\nD\tHTTP Error\n$\tERR\t$"
>
>
>
> def http_postSnapshot(host, port = 80, document = "/", snapshot = ""):
>
>         try:
>                 http = miniclient(host, port)
>
>         except Exception, e:
>
>                 if e[0] == 111:
>                         print   "Connection refused by server %s on port
> %d" % (host,port)
>
>                 raise
>
>         try:
>                 http.writeline("POST %s HTTP/1.1" % str(document))
>                 http.writeline("HOST:   %s" % str(host))
>                 http.writeline("User-Agent: GameSpyHTTP/1.0")
>                 http.writeline("Content-Type:
> application/x-www-form-urlencoded")
>                 http.writeline("Content-Length: %s" % str(len(snapshot)))
>                 http.writeline("Connection: close")
>                 http.writeline("")
>                 http.writeline(str(snapshot))
>                 http.writeline("")
>                 http.shutdown() # be nice, tell the http server we're done
> sending the request
>
>                 # Check that SnapShot Arrives.
>                 # Determine Status
>                 status = string.split(http.readline())
>                 if status[0] != "HTTP/1.1":
>                         print "MiniClient: Unknown status response (%s)" %
> str(status[0])
>
>                 try:
>                         status = string.atoi(status[1])
>                 except ValueError:
>                         print "MiniClient: Non-numeric status code (%s)" %
> str(status[1])
>
>                 #Extract Headers
>                 headers = []
>                 while 1:
>                         line = http.readline()
>                         if not line:
>                                 break
>                         headers.append(line)
>
>                 http.close() # all done
>
>                 if status == 200:
>                         print "SNAPSHOT Received: OK"
>                         returnCode = 1
>                 else:
>                         print "SNAPSHOT Received: ERROR"
>                         returnCode = 0
>
>                 return status
>
>         except Exception, e:
>                 raise
>
> class miniclient:
>         "Client support class for simple Internet protocols."
>
>         def __init__(self, host, port):
>                 "Connect to an Internet server."
>
>
>                 self.sock = socket.socket(socket.AF_INET,
> socket.SOCK_STREAM)
>                 self.sock.settimeout(30)
>
>                 try:
>                         self.sock.connect((host, port))
>                         self.file = self.sock.makefile("rb")
>
>                 except socket.error, e:
>
>                         #if e[0]        == 111:
>                         #       print   "Connection refused by server %s
> on port %d" % (host,port)
>
>                         raise
>
>
>         def writeline(self, line):
>                 "Send a line to the server."
>
>                 try:
>                         self.sock.send(line + CRLF) # unbuffered write
>
>                 except socket.error, e:
>                         if e[0] == 32 : #broken pipe
>                                 self.sock.close() # mutual close
>                                 self.sock = None
>
>                         raise e
>
>                 except socket.timeout:
>                         self.sock.close() # mutual close
>                         self.sock = None
>
>                         raise
>
>         def readline(self):
>                 "Read a line from the server.  Strip trailing CR and/or
> LF."
>
>
>                 s = self.file.readline()
>
>                 if not s:
>                         raise EOFError
>
>                 if s[-2:] == CRLF:
>                         s = s[:-2]
>
>                 elif s[-1:] in CRLF:
>                         s = s[:-1]
>
>                 return s
>
>
>         def read(self, maxbytes = None):
>                 "Read data from server."
>
>                 if maxbytes is None:
>                         return self.file.read()
>
>                 else:
>                         return self.file.read(maxbytes)
>
>
>         def shutdown(self):
>
>                 if self.sock:
>                         self.sock.shutdown(1)
>
>
>         def close(self):
>
>                 if self.sock:
>                         self.sock.close()
>                         self.sock = None
>
> ÔÚ 07-6-6£¬Âí̤·ÉÑà<honeyday.mj在gmail.com> дµÀ£º
> > ¾ÍÊÇÓÃpythonÌṩµÄurllibÕâÀàµÄ¿âÀ´Ä£ÄâÒ»¸öÉÏ´«ÎļþµÄ±íµ¥µÄÌá½»¶¯×÷£¬°Ñ±¾µØÎļþÌá½»µ½·þÎñÆ÷ÉÏ¡£
> >
> > Ó¦¸ÃÔõôдÄØ£¿
> > Ò»°ãµÄurllib.urlopenÓÃpost´ò¿ªµÄurlÔõô°ÑÎļþµÄÊý¾ÝÁ÷Ҳд¹ýÈ¥ÄØ£¿
> > ½ÓÊÕ·½ÊÇÓÃdjangoдµÄ£¬Ð»Ð»À²¡£
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
Nicholas @ Nirvana Studio
http://www.nirvanastudio.org
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070606/9cf6ef65/attachment-0001.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 18:18

Nicholas Ding nicholasdsj在gmail.com
星期三 六月 6 18:18:47 HKT 2007

Â¥ÉϵĴúÂëÊDz»¶ÔµÄ£¬ÎļþÉÏ´«ºÍÆÕͨPOST²»Ì«Ò»Ñù£¬ÄãÐèҪʹÓÃMultipart£¬²»È»ÄãÊDz»¿ÉÄÜÈÃdjangoÓÃrequest.FILESÀ´»ñÈ¡µÄ¡£

On 6/6/07, É¢Âþ¿áÄк¢ <17mxnet在gmail.com> wrote:
>
> ¸øÄã´úÂë²Î¿¼!
>
> import socket, string
>
> CRLF = "\r\n"
>
> def http_get(host, port = 80, document = "/"):
>
>         try:
>                 http = miniclient(host, port)
>
>         except Exception, e:
>
>                 if e[0] == 111:
>                         print   "Connection refused by server %s on port
> %d" % (host,port)
>
>                 raise
>
>         http.writeline("GET %s HTTP/1.1" % str(document))
>         http.writeline("HOST: %s" % host)
>         http.writeline("User-Agent:     GameSpyHTTP/1.0")
>         http.writeline("Connection: close") # do not keep-alive
>         http.writeline("")
>         http.shutdown() # be nice, tell the http server we're done sending
> the request
>
>         # Determine Status
>         status = string.split(http.readline())
>         if status[0] != "HTTP/1.1":
>                 print "MiniClient: Unknown status response (%s)" %
> str(status[0])
>
>         try:
>                 status = string.atoi(status[1])
>         except ValueError:
>                 print "MiniClient: Non-numeric status code (%s)" %
> str(status[1])
>
>         #Extract Headers
>         headers = []
>         while 1:
>                 line = http.readline()
>                 if not line:
>                         break
>                 headers.append(line)
>
>         http.close() # all done
>
>         #Check we got a valid HTTP response
>         if status == 200:
>                 return http.read()
>         else:
>                 return "E\nH\terr\nD\tHTTP Error\n$\tERR\t$"
>
>
>
> def http_postSnapshot(host, port = 80, document = "/", snapshot = ""):
>
>         try:
>                 http = miniclient(host, port)
>
>         except Exception, e:
>
>                 if e[0] == 111:
>                         print   "Connection refused by server %s on port
> %d" % (host,port)
>
>                 raise
>
>         try:
>                 http.writeline("POST %s HTTP/1.1" % str(document))
>                 http.writeline("HOST:   %s" % str(host))
>                 http.writeline("User-Agent: GameSpyHTTP/1.0")
>                 http.writeline("Content-Type:
> application/x-www-form-urlencoded")
>                 http.writeline("Content-Length: %s" % str(len(snapshot)))
>                 http.writeline("Connection: close")
>                 http.writeline("")
>                 http.writeline(str(snapshot))
>                 http.writeline("")
>                 http.shutdown() # be nice, tell the http server we're done
> sending the request
>
>                 # Check that SnapShot Arrives.
>                 # Determine Status
>                 status = string.split(http.readline())
>                 if status[0] != "HTTP/1.1":
>                         print "MiniClient: Unknown status response (%s)" %
> str(status[0])
>
>                 try:
>                         status = string.atoi(status[1])
>                 except ValueError:
>                         print "MiniClient: Non-numeric status code (%s)" %
> str(status[1])
>
>                 #Extract Headers
>                 headers = []
>                 while 1:
>                         line = http.readline()
>                         if not line:
>                                 break
>                         headers.append(line)
>
>                 http.close() # all done
>
>                 if status == 200:
>                         print "SNAPSHOT Received: OK"
>                         returnCode = 1
>                 else:
>                         print "SNAPSHOT Received: ERROR"
>                         returnCode = 0
>
>                 return status
>
>         except Exception, e:
>                 raise
>
> class miniclient:
>         "Client support class for simple Internet protocols."
>
>         def __init__(self, host, port):
>                 "Connect to an Internet server."
>
>
>                 self.sock = socket.socket(socket.AF_INET,
> socket.SOCK_STREAM)
>                 self.sock.settimeout(30)
>
>                 try:
>                         self.sock.connect((host, port))
>                         self.file = self.sock.makefile("rb")
>
>                 except socket.error, e:
>
>                         #if e[0]        == 111:
>                         #       print   "Connection refused by server %s
> on port %d" % (host,port)
>
>                         raise
>
>
>         def writeline(self, line):
>                 "Send a line to the server."
>
>                 try:
>                         self.sock.send(line + CRLF) # unbuffered write
>
>                 except socket.error, e:
>                         if e[0] == 32 : #broken pipe
>                                 self.sock.close() # mutual close
>                                 self.sock = None
>
>                         raise e
>
>                 except socket.timeout:
>                         self.sock.close() # mutual close
>                         self.sock = None
>
>                         raise
>
>         def readline(self):
>                 "Read a line from the server.  Strip trailing CR and/or
> LF."
>
>
>                 s = self.file.readline()
>
>                 if not s:
>                         raise EOFError
>
>                 if s[-2:] == CRLF:
>                         s = s[:-2]
>
>                 elif s[-1:] in CRLF:
>                         s = s[:-1]
>
>                 return s
>
>
>         def read(self, maxbytes = None):
>                 "Read data from server."
>
>                 if maxbytes is None:
>                         return self.file.read()
>
>                 else:
>                         return self.file.read(maxbytes)
>
>
>         def shutdown(self):
>
>                 if self.sock:
>                         self.sock.shutdown(1)
>
>
>         def close(self):
>
>                 if self.sock:
>                         self.sock.close()
>                         self.sock = None
>
> ÔÚ 07-6-6£¬Âí̤·ÉÑà<honeyday.mj在gmail.com> дµÀ£º
> > ¾ÍÊÇÓÃpythonÌṩµÄurllibÕâÀàµÄ¿âÀ´Ä£ÄâÒ»¸öÉÏ´«ÎļþµÄ±íµ¥µÄÌá½»¶¯×÷£¬°Ñ±¾µØÎļþÌá½»µ½·þÎñÆ÷ÉÏ¡£
> >
> > Ó¦¸ÃÔõôдÄØ£¿
> > Ò»°ãµÄurllib.urlopenÓÃpost´ò¿ªµÄurlÔõô°ÑÎļþµÄÊý¾ÝÁ÷Ҳд¹ýÈ¥ÄØ£¿
> > ½ÓÊÕ·½ÊÇÓÃdjangoдµÄ£¬Ð»Ð»À²¡£
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
Nicholas @ Nirvana Studio
http://www.nirvanastudio.org
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070606/542e5c98/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年06月06日 星期三 18:21

散漫酷男孩 17mxnet在gmail.com
星期三 六月 6 18:21:00 HKT 2007

HTTP协议很简单的。。。
稍微看看就明白了。。
这个可以自己扩展。。

在 07-6-6,Nicholas Ding<nicholasdsj在gmail.com> 写道:
> 楼上的代码是不对的,文件上传和普通POST不太一样,你需要使用Multipart,不然你是不可能让django用request.FILES来获取的。
>
>
> On 6/6/07, 散漫酷男孩 <17mxnet在gmail.com > wrote:
> > 给你代码参考!
> >
> > import socket, string
> >
> > CRLF = "\r\n"
> >
> > def http_get(host, port = 80, document = "/"):
> >
> >         try:
> >                 http = miniclient(host, port)
> >
> >         except Exception, e:
> >
> >                 if e[0] == 111:
> >                         print   "Connection refused by server %s on port
> %d" % (host,port)
> >
> >                 raise
> >
> >         http.writeline("GET %s HTTP/1.1" % str(document))
> >         http.writeline("HOST: %s" % host)
> >         http.writeline("User-Agent:     GameSpyHTTP/1.0")
> >         http.writeline("Connection: close") # do not keep-alive
> >         http.writeline("")
> >         http.shutdown() # be nice, tell the http server we're done sending
> the request
> >
> >         # Determine Status
> >         status = string.split(http.readline())
> >         if status[0] != "HTTP/1.1":
> >                 print "MiniClient: Unknown status response (%s)" %
> str(status[0])
> >
> >         try:
> >                 status = string.atoi(status[1])
> >         except ValueError:
> >                 print "MiniClient: Non-numeric status code (%s)" %
> str(status[1])
> >
> >         #Extract Headers
> >         headers = []
> >         while 1:
> >                 line = http.readline()
> >                 if not line:
> >                         break
> >                 headers.append(line)
> >
> >         http.close() # all done
> >
> >         #Check we got a valid HTTP response
> >         if status == 200:
> >                 return http.read()
> >         else:
> >                 return "E\nH\terr\nD\tHTTP Error\n$\tERR\t$"
> >
> >
> >
> > def http_postSnapshot(host, port = 80, document = "/", snapshot = ""):
> >
> >         try:
> >                 http = miniclient(host, port)
> >
> >         except Exception, e:
> >
> >                 if e[0] == 111:
> >                         print   "Connection refused by server %s on port
> %d" % (host,port)
> >
> >                 raise
> >
> >         try:
> >                 http.writeline("POST %s HTTP/1.1" % str(document))
> >                 http.writeline("HOST:   %s" % str(host))
> >                 http.writeline ("User-Agent: GameSpyHTTP/1.0")
> >                 http.writeline("Content-Type:
> application/x-www-form-urlencoded")
> >                 http.writeline("Content-Length: %s" % str(len(snapshot)))
> >                 http.writeline("Connection: close")
> >                 http.writeline("")
> >                 http.writeline(str(snapshot))
> >                 http.writeline("")
> >                  http.shutdown() # be nice, tell the http server we're
> done sending the request
> >
> >                 # Check that SnapShot Arrives.
> >                 # Determine Status
> >                 status = string.split(http.readline ())
> >                 if status[0] != "HTTP/1.1":
> >                         print "MiniClient: Unknown status response (%s)" %
> str(status[0])
> >
> >                 try:
> >                         status = string.atoi(status[1])
> >                 except ValueError:
> >                         print "MiniClient: Non-numeric status code (%s)" %
> str(status[1])
> >
> >                 #Extract Headers
> >                 headers = []
> >                 while 1:
> >                         line = http.readline()
> >                         if not line:
> >                                 break
> >                         headers.append(line)
> >
> >                  http.close() # all done
> >
> >                 if status == 200:
> >                         print "SNAPSHOT Received: OK"
> >                         returnCode = 1
> >                 else:
> >                         print "SNAPSHOT Received: ERROR"
> >                         returnCode = 0
> >
> >                 return status
> >
> >         except Exception, e:
> >                 raise
> >
> > class miniclient:
> >         "Client support class for simple Internet protocols."
> >
> >         def __init__(self, host, port):
> >                 "Connect to an Internet server."
> >
> >
> >                 self.sock = socket.socket(socket.AF_INET,
> socket.SOCK_STREAM)
> >                 self.sock.settimeout (30)
> >
> >                 try:
> >                         self.sock.connect((host, port))
> >                         self.file = self.sock.makefile("rb")
> >
> >                 except socket.error, e:
> >
> >                         #if e[0]        == 111:
> >                         #       print   "Connection refused by server %s
> on port %d" % (host,port)
> >
> >                         raise
> >
> >
> >         def writeline(self, line):
> >                 "Send a line to the server."
> >
> >                 try:
> >                         self.sock.send(line + CRLF) # unbuffered write
> >
> >                 except socket.error, e:
> >                         if e[0] == 32 : #broken pipe
> >                                  self.sock.close() #
> mutual close
> >                                 self.sock = None
> >
> >                         raise e
> >
> >                 except socket.timeout:
> >                         self.sock.close() # mutual close
> >                         self.sock = None
> >
> >                         raise
> >
> >         def readline(self):
> >                 "Read a line from the server.  Strip trailing CR and/or
> LF."
> >
> >
> >                 s = self.file.readline()
> >
> >                 if not s:
> >                         raise EOFError
> >
> >                 if s[-2:] == CRLF:
> >                         s = s[:-2]
> >
> >                 elif s[-1:] in CRLF:
> >                         s = s[:-1]
> >
> >                 return s
> >
> >
> >         def read(self, maxbytes = None):
> >                 "Read data from server."
> >
> >                 if maxbytes is None:
> >                         return self.file.read()
> >
> >                 else:
> >                         return self.file.read(maxbytes)
> >
> >
> >         def shutdown(self):
> >
> >                 if self.sock:
> >                         self.sock.shutdown (1)
> >
> >
> >         def close(self):
> >
> >                 if self.sock:
> >                         self.sock.close()
> >                         self.sock = None
> >
> > 在 07-6-6,马踏飞燕< honeyday.mj在gmail.com> 写道:
> > > 就是用python提供的urllib这类的库来模拟一个上传文件的表单的提交动作,把本地文件提交到服务器上。
> > >
> > > 应该怎么写呢?
> > > 一般的urllib.urlopen用post打开的url怎么把文件的数据流也写过去呢?
> > > 接收方是用django写的,谢谢啦。
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
>
> --
> Nicholas @ Nirvana Studio
> http://www.nirvanastudio.org
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号