Python Forum

Full Version: here is another 2.7 to 3.5 convert
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
here is the code, some of it have been converted, so like it is not it will not run on ether one.

import socket   #for sockets
import sys  #for exit
 
try:
    #create an AF_INET, STREAM socket (TCP)
    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:
    #could not resolve
    print ('Hostname could not be resolved. Exiting')
    sys.exit()
     
print ('Ip address of ') + host + ' is ' + remote_ip
 
#Connect to remote server
s.connect((remote_ip , port))
 
print ('Socket Connected to ') + host + ' on ip ' + remote_ip
I think this part is the problem:
print ('Ip address of ') + host + ' is ' + remote_ip
 
#Connect to remote server
s.connect((remote_ip , port))
 
print ('Socket Connected to ') + host + ' on ip ' + remote_ip
I been looking for a place that show the 2.7 commands = the 3.5, I have not found any thing like that yet.

What way is the best to convert the code?
Sick

I try to edit this but could find a way.
(Oct-21-2016, 04:37 PM)Blue Dog Wrote: [ -> ]What way is the best to convert the code?
Actually knowing Python help Wink
and what's the difference is between 2 and 3 it's not so much work to convert short code like this.
Use 2to3 it dos a pretty good job.
(Oct-21-2016, 04:37 PM)Blue Dog Wrote: [ -> ]I think this part is the problem:
>>> host = 'google.com'
>>> remote_ip = '111.111.111'
>>> print ('Ip address of ') + host + ' is ' + remote_ip
Ip address of 
Traceback (most recent call last):
 File "<string>", line 301, in runcode
 File "<interactive input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

# Fix
>>> print('Ip address of ' + host + ' is ' + remote_ip)
Ip address of google.com is 111.111.111

>>> # Better
>>> print('Ip address of {} is {}'.format(host, remote_ip))
Ip address of google.com is 111.111.111
I don't need a port? =  port = 80
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?
********************************************************************************************
I tryed to run the code and I got this:
File "C:\Users\renny and kite\Desktop\Python code snpppets\email_scraper.py", line 17
    Ip address of
             ^
SyntaxError: invalid syntax
[Finished in 0.1s]
************************************************************************************************

17 Ip address of    #this is where line 17 starts
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
*******************************************************************************************************
This having two type of code is driving me crazy.
You can see that I want to play around with bots. Is the a good tut. in 3.5 on making bots. I don't care what kinds, right now just want to get basic down and learned, then I can go from their?
(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))
Ok, that does work, I found a place that have a lot of the commands for 3.5.2 . I hope I can start to get my mind warped around this.
Thank you
blue dog
I found this great book on web scraper call "Web-Scraping-With-Python. Big Grin 

I been looking for something like for a while now.
Blue Dog Smile