Python Forum
here is another 2.7 to 3.5 convert
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
here is another 2.7 to 3.5 convert
#4
(Oct-21-2016, 05:33 PM)Blue Dog Wrote: what is a  remote_ip = '111.111.111, that only have 3 set of numbers, most ip have 4 set of numbers 111.111.111.111?
It could have been any number,i was just showing what was wrong with this line:
print ('Ip address of ') + host + ' is ' + remote_ip # ) wrong placement
The correct and best way:
print('Ip address of {} is {}'.format(host, remote_ip))
Have you not look into now how interactive interpreter work?


You can try this code:
import socket   
import sys

try:    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as msg:
    print('Failed to create socket. Error code: {}'.format(str(msg)))
    sys.exit()

print('Socket Created')
host = 'www.google.com'
port = 80

try:
    remote_ip = socket.gethostbyname(host)
except socket.gaierror:
    print('Hostname could not be resolved. Exiting')
    sys.exit()

print('Ip address of {} is {}'.format(host, remote_ip))
s.connect((remote_ip , port))
print('Socket Connected to {} on ip {}'.format(host, remote_ip))
Reply


Messages In This Thread
here is another 2.7 to 3.5 convert - by Blue Dog - Oct-21-2016, 04:37 PM
RE: here is another 2.7 to 3.5 convert - by snippsat - Oct-21-2016, 07:34 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020