我正在尝试使用getaddrinfo()解析URL的IP地址,但它总是返回错误的IP地址,我尝试了几个URL,结果是一样的.非常感谢任何帮助.
该程序返回IP地址:209.85.175.99返回真实IP,即74.125.39.147
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int
main()
{
char *hostname = "www.google.lk";
struct addrinfo hints, *res;
struct in_addr addr;
int err;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0) {
printf("error %d\n", err);
return 1;
}
addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
printf("ip address : %s\n", inet_ntoa(addr));
freeaddrinfo(res);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想得到一些字符串,范围从0000到9999,也就是说,我想打印以下字符串:
0000
0001
0002
0003
0004
0005
0006
....
9999
Run Code Online (Sandbox Code Playgroud)
我试图使用print "\n".join([str(num) for num in range(0, 9999)]),但失败了,我得到以下数字:
0
1
2
3
4
5
6
...
9999
Run Code Online (Sandbox Code Playgroud)
我希望python 自动添加前缀0,使数字始终保持4位数.任何人都可以帮助我吗?任何帮助赞赏.
我有一个脚本接受一个文件名作为参数而不是打开它并写一些东西.
我使用with声明:
with open(file_name, 'w') as out_file:
...
out_file.write(...)
Run Code Online (Sandbox Code Playgroud)
sys.stdout如果没有file_name提供,我现在想写什么?
我是否一定需要在函数中包装所有操作并在之前设置条件?
if file_name is None:
do_everything(sys.stdout)
else:
with open(file_name, 'w') as out_file:
do_everything(out_file)
Run Code Online (Sandbox Code Playgroud) 通常,iPhone中的标签栏选择颜色为蓝色.而不是我想改变颜色为棕色.如何更改标签栏选择颜色?我需要创建自定义tabbar控制器吗?
我有这个C代码:
typedef struct test * Test;
struct test {
void *a;
Test next;
};
Run Code Online (Sandbox Code Playgroud)
你将如何在Python中实现与此相当的东西(如果可能的话)?
class myDecorator(object):
def __init__(self, f):
print "inside myDecorator.__init__()"
f() # Prove that function definition has completed
def __call__(self):
print "inside myDecorator.__call__()"
@myDecorator
def aFunction():
print "inside aFunction()"
def main():
print "main starts....."
aFunction()
print "main ends....."
Run Code Online (Sandbox Code Playgroud)
输出:
inside myDecorator.__init__()
inside aFunction()
main starts.....
inside myDecorator.__call__()
main ends.....
Run Code Online (Sandbox Code Playgroud)
关于上述代码,我无法理解以下几点:
为什么"主要开始...."不是第一行打印?
假设我从aFunction()返回一些值,那么它将无法代替其调用,因为aFunction()它被替换为inside myDecorator.__call__()not inside aFunction().
如果列表列表中的每个元素都大于其neigbbour,那么计算最"pythonic"的方法是什么?例如
a = [[3.1, 3.13], [3.14, 3.12], [3.12, 3.1]]
Run Code Online (Sandbox Code Playgroud)
我想看看每个列表中的第一个元素(在较大列表中)是否大于第二个元素.所以对于第一项,它的假,因为3.1 <3.13.第2和第3项是真的.
我当然可以使用for循环,但是希望看到替代方法.谢谢.
任何人都知道ino_t类型的占位符是什么?我正在尝试使用printf打印出来并尝试了%d,%i,%s和其他但不能正常工作.
printf( " file name = %s, i-node number=**%d**\n", direntp->d_name, direntp->d_ino);
warning: format ‘%i’ expects argument of type ‘int’, but argument 3 has type ‘__ino_t’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)
假设我的其他代码是正确的.大多数示例仅显示如何打印名称,但不显示inode编号.我也搜过很多地方.
提前致谢
我是编程新手,目前正在学习 Python。我想编写一个程序:
N = input("Please enter an non-negative even integer: ") #request user to input
Run Code Online (Sandbox Code Playgroud)
标准检查代码是:
N == int(N) #it is integer
N % 2 == 0 #it is even number
N >=0 # it is non-negative number
Run Code Online (Sandbox Code Playgroud)
但是我应该如何将它们结合起来?
在C中,通常不允许数组的大小为0(除非我使用一个或其他编译器端扩展).
OTOH,有长度可能为0的VLA.
他们被允许吗?
我在谈论以下代码:
void send_stuff()
{
char data[4 * !!flag1 + 2 * !!flag2];
uint8_t cursor = 0;
if (flag1) {
// fill 4 bytes of data into &data[cursor]
cursor += 4;
}
if (flag2) {
// fill 2 bytes of data into &data[cursor]
cursor += 2;
}
}
Run Code Online (Sandbox Code Playgroud)
结果是一个data长度为0,2,4或6 的数组,具体取决于标志的组合.
现在的问题是:对于数组结果长度为0的情况,这个有效的代码是什么?
python ×5
c ×4
c99 ×1
file ×1
getaddrinfo ×1
ios ×1
ip ×1
ip-address ×1
ipad ×1
iphone ×1
linux ×1
numbers ×1
python-2.7 ×1
python-3.x ×1
sockets ×1
stdout ×1
string ×1