Shinyshell Community Forums > Coding >
Pikachu's Python Thread


[1]


October 04 02009, 03:26 GMT
Pikachu
Kelp is good!

Pikachu's avatar
Location: California
Post count: 50
us
Okay this is gonna be my official thread for Python questions.

I'm trying to figure out how to detect if someone is using an iPhone/iPod touch safari browser in Python, but I'm not sure how to do so. I know you can do it like this in PHP:

<?php 
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
if ($browser == true) { echo 'Code You Want To Execute'; }
?>
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
if ($browser == true) { echo 'Code You Want To Execute'; }
?>]]>


I would I do so it in Python? I know it has something to do with HTTP user agent, but I don't understand it too well.

______________________________

Linux | Chrome | Python | Chuck

October 04 02009, 11:37 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173

Okay, so first of all, HTTP_USER_AGENT is a CGI variable that contains the name of the program being used to access the page, and also has some data about the device/operating system of the client.

For the iPhone, I believe it's something like this:
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en).

So I'll use Python's string.find() method, which is similar to PHP's strpos(). Note: it returns -1 if the string is not found. This is also true for the code you wrote. The only reason it works is that iPhone will never be at the start of the string. So, you should compare it with -1, not with 0.

import os 

iphone = False

try: user_agent = os.environ["HTTP_USER_AGENT"]
except KeyError: pass
else:
if user_agent.lower().find("iphone") != -1:
iphone = True

# now use it to do whatever you want
if iphone: print "You're using an iPhone to browse!"
else: print "Hi, welcome."
import os

iphone = False

try: user_agent = os.environ["HTTP_USER_AGENT"]
except KeyError: pass
else:
if user_agent.lower().find("iphone") != -1:
iphone = True

# now use it to do whatever you want
if iphone: print "You're using an iPhone to browse!"
else: print "Hi, welcome."
]]>


It's possible that there is a shorter way of doing this. If anyone knows, I'd like to know.

October 04 02009, 12:54 GMT
Faltzer
Member

Faltzer's avatar
Location: Glendale, New York
Post count: 38
us
hey thats a cool example bro
______________________________
FHQ

October 06 02009, 04:00 GMT
SpaceMan
Member

SpaceMan's avatar
Location: Earth
Post count: 32
ca
What does import and that dot mean?

October 06 02009, 07:28 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173

SpaceMan said
What does import and that dot mean?

What dot?

October 07 02009, 21:11 GMT
Faltzer
Member

Faltzer's avatar
Location: Glendale, New York
Post count: 38
us
http://docs.python.org/reference/simple_stmts.html#grammar-token-import_stmt

The dot is a way of accessing an objects attributes/methods.
______________________________
FHQ

October 08 02009, 17:54 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173
hr
He's a Java coder, so I'm pretty sure he's well acquainted with both the dot and import. I'm also reasonably sure he knows Python, so I had no clue what he was on about.

October 09 02009, 00:07 GMT
Pikachu
Kelp is good!

Pikachu's avatar
Location: California
Post count: 50
us
Another question: I'd like to learn to use Python for desktop programming. Is there any tutorials I can find that will help with such a task? I know that electron also programs desktop programs using Python, and I was interested in learning.

Oh and thanks for the help with the iPhone thingy. I'll try it out when I get the chance.
______________________________

Linux | Chrome | Python | Chuck

October 09 02009, 20:52 GMT
Faltzer
Member

Faltzer's avatar
Location: Glendale, New York
Post count: 38
us
http://www.wxpython.org/
______________________________
FHQ

October 11 02009, 19:38 GMT
SpaceMan
Member

SpaceMan's avatar
Location: Earth
Post count: 32
ca
What is OOP? Now I can no longer program properly.

October 11 02009, 19:52 GMT
Peter*
A Pythonic One

Peter's avatar
Location: US
Post count: 99

OOP means Object-Oriented Programming.

October 12 02009, 04:19 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173
hr
SpaceMan and everyone else, though SpaceMan in particular: stop going off topic in stupid ways.

@Pikachu: I don't really know of any good tutorials, actually I've read very few tutorials on anything Python-related in general. You should check out the Tkinter library to learn how to create some simple GUIs, and wxPython's the next logical step. I'm sure you can find some good tutorials for both via Google.

The original question was answered, and the subsequent ones now are too. Topic closed.


[1]



Forum Information
  Currently Active Members [detailed] (0 members and ? guests)
-
Forum Statistics
Topics: 0, Posts: 0, Members: 108.
Welcome to our newest member, adamthephantump
Most members online was 5, on August 28 2009, at 21:49:28.
Legend
    Forums with unread topics in them are indicated by a strong yellow colouring around the forum icon.
    Forums with no unread topics have the standard pale yellow colouring around the forum icon.
    Forums with a blue arrow icon will redirect you to a non-forum page.
    Locked forums have a little padlock by their icon. You won't be able to post in these forums.
Shinyshell Home | Contact | Staff List | Archive | Top 

Conventional Login

Don't have an account? You may want to create one.

OpenID Login
OpenID login and registration is usable, but not finished.
What is OpenID?
Search

(advanced search)
Site Stats
  Total members: 108
  Latest member: adamthephantump
  Members currently online: 0
  Most online: 5 - Aug 28, 2009 (21:49)
  Front page hits: 87997
Developer info
  Site version: 3.5 Alpha
  16 queries - 9 templates
Under the Spotlight
Collide Site
Collide make fabulously dreamy electronic-industrial music, they're one of my favourite bands! Give them a chance to take control of your life - myspace | youtube - "Euphoria".

Collide Site - Hits: 4596

5/5 (2) | Rate this site?