1

我试图通过一个简单的 python 代码提交我的解决方案,该代码使用我的用户名、密码、文件名、语言选择和问题代码代表我将解决方案提交给 spoj。

到目前为止,我已经走了,

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 中有没有办法“记住”“我已经登录”成功,现在让我提交问题”,这样就不会看到“需要授权”提示。

4

2 回答 2

0

您提交文件的方法不正确。您传递的是带有文件名的字符串,而不是实际文件作为subm_file. 您需要发布文件的内容。

我个人使用posterPOST 文件:http ://atlee.ca/software/poster/

这是一个相关的 SO 问题:Send file using POST from a Python script

另外,查看源代码,我认为您想将其发布到http://www.spoj.com/submit/complete/,而不是.../submit/. .../submit/是左侧登录表单的操作。.../submit/complete/是表单提交代码的动作。

于 2012-12-24T11:27:10.767 回答
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)",
}
于 2012-12-24T14:29:22.007 回答