3

This is my code where i have handled the exceptions the way it is written in python documentation but sometimes i do not know what happens but my code stuck at this print "SERVER RESPONSE" line and does not continue any forward and I have to forcibly stop it. It does not even throw any exception.It just came to halt after this print "SERVER RESPONSE" in try: block is printed on terminal.

def upload(filename1,sampleFile,unknown_path,predictiona,predictionb):

    curr_time = (time.strftime("%H:%M:%S"))
    curr_day =  (time.strftime("%Y-%m-%d"))

    register_openers()

    datagen, headers = multipart_encode({"sampleFile": open(sampleFile), "name": filename1, "userID":'19','date': curr_day,'time': curr_time})
    print"header",headers
    request = urllib2.Request("http://videoupload.hopto.org:5000/api/Sync_log", datagen, headers)
    try:
        print "SERVER RESPONSE"
        response = urllib2.urlopen(request)
        html=response.read()

    except URLError , e:
        if hasattr(e, 'reason'):
            print 'We failed to reach a server.'
            print 'Reason: ', e.reason
        elif hasattr(e, 'code'):
            print 'The server couldn\'t fulfill the request.'
            print 'Error code: ', e.code
    else:
        print "response ",response 
        print "html ",html
irum zahra
  • 417
  • 1
  • 8
  • 17

2 Answers2

0

By default urllib2.urlopen() performs the request without timing out. Maybe the server simply isn't answering the request?

rincewind
  • 2,502
  • 19
  • 26
-1

Perhaps it is successfully performing the try block, and ending? What makes you think that it is failing? Additionally, your final else: is unindented, is this in the program or just incorrect stackoverflow formatting?

clubby789
  • 2,543
  • 4
  • 16
  • 32
  • after executing `print "SERVER RESPONSE"` it stops working, it pauses the execution. secondly, my final `else` is right below the `try` block. their indentation matches. If there is something i am missing please do tell me. – irum zahra Feb 14 '17 at 20:36
  • How do you know that it has been paused rather than simply finishing? – clubby789 Feb 14 '17 at 20:37
  • because i have just posted a part of my whole program. Its big and doing other things too after executing this function.. and please read my previous comment i have edited it again. – irum zahra Feb 14 '17 at 20:39
  • and yes it does not give me server response if the try block was successful it should upload the photo to the server and in return it should print a response. – irum zahra Feb 14 '17 at 20:41
  • Else statements aren't used with try/except. To catch all errors, simply use except: – clubby789 Feb 14 '17 at 20:50
  • my code is not executing this line `response = urllib2.urlopen(request)` that I am sure of. And thank you so much for replying me. And I am new to python. – irum zahra Feb 14 '17 at 20:58
  • and where should i put the code in `else` part.. in `try` block ? – irum zahra Feb 14 '17 at 20:59
  • If you are following an if statement in your try block, have the else at the same indentation level – clubby789 Feb 14 '17 at 21:00
  • i don't know much but i read in a python documentation that we can use else with try/except... so i wrote my code like that. – irum zahra Feb 14 '17 at 21:00
  • As far as I know it is not possible to do this. If what your are trying to do with the else block is catch any errors, then you should use `except:`. This functions similarly to an else statement with an if. – clubby789 Feb 14 '17 at 21:01
  • that if statement which is in my code comes under `except:` but `else:` is with `try:... except: ... else` – irum zahra Feb 14 '17 at 21:02
  • no what i am doing is that if `try:` is successful execute `else:` otherwise execute `except:` – irum zahra Feb 14 '17 at 21:04
  • `try:... except: ... else` is not a valid format. Instead, use `try:... except URLError , e: .... except:` – clubby789 Feb 14 '17 at 21:04
  • problem is it goes into the `try:` prints its first line but does not move forward and never comes out of `try` block – irum zahra Feb 14 '17 at 21:05
  • Ah, I see. The `else:` is not necessary, as any failure in the `try:` block will cause it to go straight to `except:`. Move everything in the `else:` to the `try:` – clubby789 Feb 14 '17 at 21:06
  • ok lemme edit my code and i will get back to you soon. And thank you so much :) – irum zahra Feb 14 '17 at 21:06
  • its same it has not solved the problem it goes into try, prints first line and pause there, nor it comes out of it neither catch exception. is there any other way to counter this ? – irum zahra Feb 14 '17 at 21:12
  • Try changing except URLError , e: to simply except: ; its possible that there is an unhand led error happening – clubby789 Feb 14 '17 at 21:16
  • `try: print "SERVER RESPONSE" response = urllib2.urlopen(request) html=response.read() print "response ",response print "html ",html except: print "Unexpected error occured !"` – irum zahra Feb 14 '17 at 21:23
  • Sorry, I have no idea what the problem is. There doesn't seem to be anything that could cause issues. – clubby789 Feb 14 '17 at 21:26
  • hmm... it means i am doomed..But hey thank you so much, you helped me a lot. – irum zahra Feb 14 '17 at 21:28
  • No problem. The only thing I can think of now is to try commenting out or using try/except statements on more lines to see if something else is occuring – clubby789 Feb 14 '17 at 21:29
  • Just had a thought; are you leaving the program to run for long enough? It may just be taking a while for the request to finish – clubby789 Feb 14 '17 at 21:30
  • Not really the problem here, but `else` is a completely valid clause in a `try` statement. (From the docs: The optional `else` clause is executed if and when control flows off the end of the `try` clause. Exceptions in the `else` clause are not handled by the preceding `except` clauses.) – rincewind Feb 14 '17 at 22:16
  • normally it doesn't take long time for completion... It is not performing any task that takes too long to complete. – irum zahra Feb 15 '17 at 06:34