#1
  1. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13

    Post Script wont run after migrating to the new VPS enviroment


    This is what iam tryign now since the function ishish proposed wont help me.

    Code:
    try: 
      #find the needed counter for the page URL 
      if os.path.exists( path + page ) or os.path.exists( cgi_path + page ): 
          cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) 
          data = cur.fetchone()                #URL is unique, so should only be one 
    except: 
      print( repr(e) )
    Code:
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] Original exception was: 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] Traceback (most recent call last): 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120]   File "/home/nikos/public_html/cgi-bin/metrites.py", line 174, in <module> 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120]     cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120]   File "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120]     query = query % escaped_args 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] TypeError: unsupported operand type(s) for %: 'bytes' and 'str' 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] During handling of the above exception, another exception occurred: 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] Traceback (most recent call last): 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120]   File "/home/nikos/public_html/cgi-bin/metrites.py", line 177, in <module> 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120]     print( repr(e) ) 
    [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] NameError: name 'e' is not defined
    Any help about the error would be appreciated so i cna manage to run the script.
    What is now proved was once only imagined!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    You're running this script in Python 3; was it running in Python 2 previously?

    What database library are you using? It looks to me like the db library doesn't properly support Python 3. It's interpreting your query string as a bytes object.

    Also, your exception statement is wrong, you have:
    Code:
    except:
        print(repr(e))
    But what's e? If "e" is supposed to be the exception, you need to specify it:
    Code:
    except Exception as e:
        print(repr(e))
  4. #3
  5. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Hello admoore,

    In the previous VPS env i was also under version 3.3.2 liek iam now.

    The database in use was also 'pymysql' as it is now.

    You were right about my syntax i should explicitly use 'Exception as e'

    Code:
    		try:
    			#find the needed counter for the page URL
    			if os.path.exists( path + page ) or os.path.exists( cgi_path + page ):
    				cur.execute('''SELECT ID FROM counters WHERE url = %s''', page )
    				data = cur.fetchone()		#URL is unique, so should only be one
    		except Exception as e:
    			print( repr(e) )
    produces the output you see at http://superhost.gr/

    But i have no clue as to why not work in the new VPS while in the same installation of python was working in the old vPS.
    What is now proved was once only imagined!
  6. #4
  7. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Any ideas my friend?
    What is now proved was once only imagined!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    See this stack overflow thread; looks like a pymysql bug.

    http://stackoverflow.com/questions/16324734/impossible-to-insert-data-with-pymysql-when-i-use-parameter
  10. #6
  11. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Indeed:

    File "/usr/local/.../pymysql/cursors.py", line 96, in execute
    query = query % conn.escape(args)
    TypeError: unsupported operand type(s) for %: 'bytes' and 'tuple'
    Changing line 96 in cursors.py to

    query = query.decode(charset) % conn.escape(args)
    solved the problem.
    Last edited by Nik; August 29th, 2013 at 01:22 AM.
    What is now proved was once only imagined!
  12. #7
  13. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Code:
    [Thu Aug 29 06:35:31 2013] [error] [client 108.162.229.127] Traceback (most recent call last):
    [Thu Aug 29 06:35:31 2013] [error] [client 108.162.229.127]   File "/home/nikos/public_html/cgi-bin/metrites.py", line 188, in <module>
    [Thu Aug 29 06:35:31 2013] [error] [client 108.162.229.127]     cur.execute('''INSERT INTO visitors (counterID, host, city, useros, browser, lastvisit) VALUES (%s, %s, %s, %s, %s, %s)''', (cID, host, city, useros, browser, date) )
    [Thu Aug 29 06:35:31 2013] [error] [client 108.162.229.127] NameError: name 'city' is not defined
    but the code of variable city is:

    Code:
    try:
    	gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
    	city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
    	host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
    except Exception as e:
    	host = repr(e)
    Can ypou make a guess as to why the city doesnt have a value?
    What is now proved was once only imagined!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by Nik
    Code:
    [Thu Aug 29 06:35:31 2013] [error] [client 108.162.229.127] Traceback (most recent call last):
    [Thu Aug 29 06:35:31 2013] [error] [client 108.162.229.127]   File "/home/nikos/public_html/cgi-bin/metrites.py", line 188, in <module>
    [Thu Aug 29 06:35:31 2013] [error] [client 108.162.229.127]     cur.execute('''INSERT INTO visitors (counterID, host, city, useros, browser, lastvisit) VALUES (%s, %s, %s, %s, %s, %s)''', (cID, host, city, useros, browser, date) )
    [Thu Aug 29 06:35:31 2013] [error] [client 108.162.229.127] NameError: name 'city' is not defined
    but the code of variable city is:

    Code:
    try:
    	gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
    	city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
    	host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
    except Exception as e:
    	host = repr(e)
    Can ypou make a guess as to why the city doesnt have a value?
    Well, gee, I guess something is going wrong in the try block and the value isn't getting assigned. We can't know what, because you've basically silenced the error.

    You need to rethink the logic in this code. You're "try"ing to assign these three variables, and if something goes wrong and throws an exception, you assign the string representation of the exception to one of the variables (leaving the rest undefined).

    What's the point of using try/except in this way? If the code can't properly continue without the "try" block statements succeeding, why squash the exception at all? Let it blow up and throw a trace so you can debug. There's no point in catching an exception if you have no plan for actually dealing with it in a way that lets the code continue.
  16. #9
  17. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Indeed.

    Trying to troubleshoot lead me tlo the error which was a broken download of GeoIPCity.dat

    wget -N url from scratch solved the issue.

    Ypu are right about the way of handling the exception, i first catch it and then silence itm at the same time.

    I will just let it blow up by printing out the error immediately.
    What is now proved was once only imagined!

IMN logo majestic logo threadwatch logo seochat tools logo