Author Topic: Python Code Help  (Read 1048 times)

0 Members and 1 Guest are viewing this topic.

Offline Coto

  • Serf
  • *
  • Posts: 21
  • Cookies: -37
    • View Profile
Python Code Help
« on: January 16, 2016, 12:19:15 pm »
Hey, folks. I wanted to make a script, where you input a URL, and the Terminal Pings it.

My current Code:

Code: [Select]
server = input("Server to perform the Ping on: ")

os.system("gnome-terminal -e 'ping -i 2' + str(server)")

But it doesn't work, no matter what I try... I want it to work like the way I've coded it, above, because there's a bunch more lines behind this, but this is where I need help.

Thanks.
« Last Edit: January 16, 2016, 12:19:30 pm by Coto »

Offline TheWormKill

  • EZ's Scripting Whore
  • Global Moderator
  • Knight
  • *
  • Posts: 257
  • Cookies: 66
  • The Grim Reaper of Worms
    • View Profile
Re: Python Code Help
« Reply #1 on: January 16, 2016, 12:24:21 pm »
I won't tell you what you did wrong because you obv. didn't do any basic debugging of your script.
However, I recommend that you print the stuff you pass to the os.system() call. If you are done with that, come back here and post your working code, as I see it will have some room for improvements.
Stuff I did: How to think like a superuser, Iridium

He should make that "Haskell"
Quote
<m0rph-is-gay> fuck you thewormkill you python coding mother fucker

Offline Coto

  • Serf
  • *
  • Posts: 21
  • Cookies: -37
    • View Profile
Re: Python Code Help
« Reply #2 on: January 16, 2016, 01:41:31 pm »
I won't tell you what you did wrong because you obv. didn't do any basic debugging of your script.
However, I recommend that you print the stuff you pass to the os.system() call. If you are done with that, come back here and post your working code, as I see it will have some room for improvements.

I've got everything to work. What's troubling me, however, is I want the script to ask for the URL to ping, and use what I've responded here:

os.system("gnome-terminal -e 'ping -i 2' + str(server)")
as in
os.system("gnome-terminal -e 'ping -i 2' + [URL I responded to the Terminal asking for the URL to ping]")

Offline 0pt1musPr1m3

  • EZ's Asshole
  • Peasant
  • *
  • Posts: 89
  • Cookies: 90
  • Certified Asshole
    • View Profile
Re: Python Code Help
« Reply #3 on: January 16, 2016, 02:47:13 pm »
You obviously have failed at actually trying. One of the first things all python tutorials teach you is how to ask the user for raw_input

Code: (python) [Select]
#!/usr/bin/env python

import os

url = raw_input ("what is the site?")
response = os.system("ping -c 1 " + url)

if response == 0:
   print url, 'is up!'
else:
   print url, 'is down!'

And FYI I suck at coding
« Last Edit: January 20, 2016, 11:14:57 pm by 0pt1musPr1m3 »
Don't measure yourself by what you have accomplished, but by what you should have accomplished with your ability.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Python Code Help
« Reply #4 on: January 16, 2016, 06:17:20 pm »
Hey, folks. I wanted to make a script, where you input a URL, and the Terminal Pings it.

My current Code:

Code: [Select]
server = input("Server to perform the Ping on: ")

os.system("gnome-terminal -e 'ping -i 2' + str(server)")

But it doesn't work, no matter what I try... I want it to work like the way I've coded it, above, because there's a bunch more lines behind this, but this is where I need help.

Thanks.

You can't embed variables the way your trying.

Code: [Select]
os.system("gnome-terminal -e 'ping -i 2' + str(server)")
Should be either this:
Code: [Select]
os.system("gnome-terminal -e 'ping -i 2" + str(server) + "'")

or much cleaner

os.system("gnome-terminal -e 'ping -i 2 %s'" % str(server))

What version of python your using matters. If Python3, then input() returns a string anyway. If Python2, you need to be using raw_input() to get  a string.

Also, os.system() is going to print to stdout, in order to catch the output to use in your code you need either use:
Code: [Select]
x = os.popen(<your command here>).read())
print(x)

or use subprocess:
Code: [Select]
proc = subprocess.Popen(["ping", "-i", "2", server], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print(out)
« Last Edit: January 16, 2016, 06:17:42 pm by techb »
>>>import this
-----------------------------

Offline gray-fox

  • Knight
  • **
  • Posts: 208
  • Cookies: 52
    • View Profile
Re: Python Code Help
« Reply #5 on: January 16, 2016, 07:22:57 pm »
You obviously have failed at actually trying. One of the first things all python tutorials teach you is how to ask the user for raw_input

Just wanted to point out that if op is using python3 then input() is valid instead of raw_input().

But I get that usually when people use just word "python" they mean python2.


edit: Just noticed that techb already pointed this out.
« Last Edit: January 16, 2016, 07:30:16 pm by gray-fox »

Offline Coto

  • Serf
  • *
  • Posts: 21
  • Cookies: -37
    • View Profile
Re: Python Code Help
« Reply #6 on: January 16, 2016, 07:57:57 pm »
Yea, I'm using Python 2.7, not 3.

Offline Coto

  • Serf
  • *
  • Posts: 21
  • Cookies: -37
    • View Profile
Re: Python Code Help
« Reply #7 on: January 16, 2016, 08:54:15 pm »
Also, what if I also want to specify an input with Timeout?

This is what I've done:

server = raw_input("Server to perform the Ping on [IP/URL]: ")
timeout = raw_input("Ping length time [Seconds]: ")

os.system("gnome-terminal -e 'ping -w %d -i 2 %s'" % str(server) % str(timeout))

So, basically, I also wanna fit the timeout string too. How do I do that? (The above code obv. doesn't work)

Also, how do I get all that code into a

for x in range(0, 3): ? So that it would open 3 Terminal windows and do that Ping.

It surprisingly didn't work for me for some reason, where as it just did before...  :o

Offline TheWormKill

  • EZ's Scripting Whore
  • Global Moderator
  • Knight
  • *
  • Posts: 257
  • Cookies: 66
  • The Grim Reaper of Worms
    • View Profile
Re: Python Code Help
« Reply #8 on: January 16, 2016, 09:02:46 pm »
Given the nature of your questions, I strongly suggest that you check syntax and semantics of the python language. You seem to lack basic understanding of string literals, concepts of command line interfaces etc.
That's nothing to be ashamed of, as everyone starts somewhere, but you should educate yourself in the first place.
Stuff I did: How to think like a superuser, Iridium

He should make that "Haskell"
Quote
<m0rph-is-gay> fuck you thewormkill you python coding mother fucker

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Python Code Help
« Reply #9 on: January 16, 2016, 09:24:22 pm »
I'm not helping anymore until you learn the language. It is obvious you haven't read anything other than maybe a 10min tutorial or something equally as bad.

Do as TheWormKill said, you don't know enough to be trying this. You need to learn more of the basics.
>>>import this
-----------------------------

Offline b00ms1ang

  • Peasant
  • *
  • Posts: 65
  • Cookies: -8
  • Oh
    • View Profile
Re: Python Code Help
« Reply #10 on: January 18, 2016, 03:58:42 pm »
https://www.python.org/about/gettingstarted/

Don't be a kiddie man, learn your language
Oh...