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
#1
here is the code, some of it have been converted, so like it is not it will not run on ether one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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:
1
2
3
4
5
6
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.
Reply
#2
(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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>> 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
Reply
#3
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?
Reply
#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:
1
print ('Ip address of ') + host + ' is ' + remote_ip # ) wrong placement
The correct and best way:
1
print('Ip address of {} is {}'.format(host, remote_ip))
Have you not look into now how interactive interpreter work?


You can try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
#5
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
Reply
#6
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
Reply


Forum Jump:

User Panel Messages

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