1

I was trying to submit my solutions all through a simple python code which takes my username,password, filename, language choice and problem code to submit the solution to spoj on my behalf.

I've gone thus so far,

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()`

I am expecting "Solution Submitted" to be displayed but instead I get Authorisation Required. But when I try just to login, it does.

Is there a way to enter the Submission after logging in ie the POST method which i am using shoul resume at the page where it logged in successfully, in other words, is there a way in python to "remember" that "I have logged in successfully, now let me submit the problem" So that no "Authorisation Required" prompt is seen.

mjnovice
  • 172
  • 11

2 Answers2

0

Your method for file submission is incorrect. You're passing a string with the filename, not the actual file as your parameter for subm_file. You need to POST contents of the file.

I personally use poster to POST files: http://atlee.ca/software/poster/

Here's a related SO question: Send file using POST from a Python script

Also, looking at the source, I think you want to POST it to http://www.spoj.com/submit/complete/, not .../submit/. .../submit/ is the action of the form for the login on the left-hand side. .../submit/complete/ is the action of the form for submitting the code.

Community
  • 1
  • 1
shashwat
  • 992
  • 1
  • 13
  • 27
  • yes, I want it to submit it to .../submit/complete but i am unable to do so. It says authorisation required meaning to say that it forgets that i am logged in. please tell how to remember that i am logged in so i can proceed with the submission???? – mjnovice Dec 24 '12 at 11:30
  • I'm not completely sure, but the way I'd do it is I'd POST the login details to `/submit/` and store the cookies. Then I'd send the cookies with my code submission to `/submit/complete/` – shashwat Dec 24 '12 at 11:34
  • I don't get it. I coded it in 5 minutes. I didn't even need to use cookies.. just what I mentioned in my solution. **EDIT:** My bad. I've made one more change. For C (gcc 4.3.2), you'll have to use `'lang':'11'` since the value won't be the string, but the assigned value. – shashwat Dec 24 '12 at 14:19
  • import httplib,urllib import urllib2 import poster import cookielib opener = poster.streaminghttp.register_openers() opener.add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) params = {'file': open("/home/mj/code/uu.c", "rb"),'login_user':'name','password':'223021','lang':'11','problemcode':'TEST'} datagen, headers = poster.encode.multipart_encode(params) upload_url="https://www.spoj.com/submit/complete" request = urllib2.Request(upload_url, datagen, headers) result = urllib2.urlopen(request) page=result.read() print page – mjnovice Dec 24 '12 at 14:47
  • is the above code what ur refereing too. it doesnt work. Check it for urself. it does not work – mjnovice Dec 24 '12 at 14:52
  • Here's my code. I didn't need to use cookiejar, since, as you mentioned, the username and password can be POSTed as well. http://pastie.org/5572405 – shashwat Dec 24 '12 at 14:53
  • urllib2 needs `http://www` as the start. End with `/`. Your url is incomplete. Also, the file goes to `subm_file` not `file` – shashwat Dec 24 '12 at 14:57
  • Thanx a lot shashwat :D your code is working, but what's the use of register openers which i was missing? – mjnovice Dec 24 '12 at 15:13
  • http://atlee.ca/software/poster/ <- Poster Documentation; It mentions that register_openers() registers the streaming http handles with urllib2, to override its default handlers – shashwat Dec 24 '12 at 15:18
0

Your params are not encoded using Content-type "multipart/form-data". I've tried sending the request using "application/x-www-form-urlencoded" content type instead and it works:

#!/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))

Languages

{
    "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)",
}
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Your solution is specific for the TEST problem. I think OP wants a general solution. – shashwat Dec 24 '12 at 14:33
  • @shashwat: The OP's problem is the incorrect http request. The code above works for any spoj problem if you provide appropriate `problemcode`, `lang`, and `file` that are specific for each solution. – jfs Dec 24 '12 at 14:45