我试图通过一个简单的python代码提交我的解决方案,该代码使用我的用户名,密码,文件名,语言选择和问题代码来代表我提交解决方案。
到目前为止我已经走了,
import httplib,urllib
import urllib2
params=urllib.urlencode({'login_user':'username','password':'yourpassword','lang':'C (gcc 4.3.2)','problemcode':'TEST','subm_file':'/home/mj/code/uu.c'})
headers={"Content-type": "multipart/form-data"}
req=urllib2.Request("http://www.spoj.com/submit/",params)
thepage=urllib2.urlopen(req)
data=thepage.read()
newf=open("wer.html","w")
newf.write(data)
newf.close()`
我希望显示“已提交解决方案”,但我需要获得授权。但是,当我尝试登录时,确实如此。
有没有办法在登录后进入提交,即我正在使用的POST方法在其成功登录的页面上恢复,换句话说,是否有一种方法在python中“记住”“我已成功登录,现在让我提交问题“这样就不会出现”需要授权“提示。
答案 0 :(得分:0)
您提交文件的方法不正确。您传递的是带有文件名的字符串,而不是实际文件作为subm_file
的参数。您需要POST文件的内容。
我个人使用poster
来POST文件:http://atlee.ca/software/poster/
以下是相关的SO问题:Send file using POST from a Python script
此外,查看来源,我认为您要将其发布到http://www.spoj.com/submit/complete/
,而不是.../submit/
。 .../submit/
是左侧登录表单的操作。 .../submit/complete/
是提交代码的表单的操作。
答案 1 :(得分:0)
您的params
未使用Content-type
"multipart/form-data"
进行编码。我尝试使用"application/x-www-form-urlencoded"
内容类型发送请求,但它确实有效:
#!/usr/bin/env python
import re
import webbrowser
from netrc import netrc
from urllib import urlencode, quote
from urllib2 import urlopen
user, _, password = netrc().authenticators('spoj.com') # read from ~/.netrc
r = urlopen("https://www.spoj.com/submit/complete/", # no certificate test
data=urlencode(dict(
login_user=user,
password=password,
problemcode="TEST", # problem id
lang="116", # Python 3 (see languages below)
submit="Send",
file="for s in iter(input, '42'): print(s)")))
m = re.search(r'"newSubmissionId" value="(\d+)"/>', r.read()) # XXX dirty
print("submission id %d" % int(m.group(1)))
webbrowser.open("https://www.spoj.com/status/%s/" % quote(user))
{
"7": "ADA 95 (gnat 4.3.2)",
"13": "Assembler (nasm 2.03.01)",
"104": "Awk (gawk-3.1.6)",
"28": "Bash (bash-4.0.37)",
"12": "Brainf**k (bff 1.0.3.1)",
"11": "C (gcc 4.3.2)",
"27": "C# (gmcs 2.0.1)",
"41": "C++ (g++ 4.3.2)",
"1": "C++ (g++ 4.0.0-8)",
"34": "C99 strict (gcc 4.3.2)",
"14": "Clips (clips 6.24)",
"111": "Clojure (clojure 1.1.0)",
"31": "Common Lisp (sbcl 1.0.18)",
"32": "Common Lisp (clisp 2.44.1)",
"20": "D (gdc 4.1.3)",
"36": "Erlang (erl 5.6.3)",
"124": "F# (fsharp 2.0.0)",
"5": "Fortran 95 (gfortran 4.3.2)",
"114": "Go (gc 2010-07-14)",
"21": "Haskell (ghc 6.10.4)",
"16": "Icon (iconc 9.4.3)",
"9": "Intercal (ick 0.28-4)",
"24": "JAR (JavaSE 6)",
"10": "Java (JavaSE 6)",
"35": "JavaScript (rhino 1.7R1-2)",
"26": "Lua (luac 5.1.3)",
"30": "Nemerle (ncc 0.9.3)",
"25": "Nice (nicec 0.9.6)",
"56": "Node.js (0.8.11)",
"8": "Ocaml (ocamlopt 3.10.2)",
"22": "Pascal (fpc 2.2.4)",
"2": "Pascal (gpc 20070904)",
"3": "Perl (perl 5.12.1)",
"54": "Perl 6 (rakudo-2010.08)",
"29": "PHP (php 5.2.6)",
"19": "Pike (pike 7.6.112)",
"15": "Prolog (swipl 5.6.58)",
"4": "Python (python 2.7)",
"116": "Python 3 (python 3.2.3)",
"126": "Python 3 nbc (python 3.2.3 nbc)",
"17": "Ruby (ruby 1.9.3)",
"39": "Scala (scala 2.8.0)",
"33": "Scheme (guile 1.8.5)",
"18": "Scheme (stalin 0.11)",
"46": "Sed (sed-4.2)",
"23": "Smalltalk (gst 3.0.3)",
"38": "Tcl (tclsh 8.5.3)",
"62": "Text (plain text)",
"6": "Whitespace (wspace 0.3)",
}